admin管理员组

文章数量:1332649

I have problem to set or show date in datepicker textbox at firsttime I have two textboxes, the one is for datepicker and the second is for alternate format.

<input type="text" id="birth_date_display" />
<input type="text" id="birth_date" />

and scripts like this

$( "#birth_date_display" ).datepicker({
 dateFormat: "d M, yy",
 altField: "#birth_date",
 altFormat: "yy-mm-dd"
});

my question is, How if i have String like: var datebirth="2011-08-16"
then i want to set to the textbox.
i had try to make script like this:

document.getElementById("birth_date_display").value = datebirth; //it's NOT works
document.getElementById("birth_date").value         = datebirth; //it's works

thanks :)

I have problem to set or show date in datepicker textbox at firsttime I have two textboxes, the one is for datepicker and the second is for alternate format.

<input type="text" id="birth_date_display" />
<input type="text" id="birth_date" />

and scripts like this

$( "#birth_date_display" ).datepicker({
 dateFormat: "d M, yy",
 altField: "#birth_date",
 altFormat: "yy-mm-dd"
});

my question is, How if i have String like: var datebirth="2011-08-16"
then i want to set to the textbox.
i had try to make script like this:

document.getElementById("birth_date_display").value = datebirth; //it's NOT works
document.getElementById("birth_date").value         = datebirth; //it's works

thanks :)

Share asked Aug 16, 2011 at 5:41 bungditobungdito 3,6204 gold badges32 silver badges38 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You want to use the setDate method on the DatePicker:

$( "#birth_date_display" ).datepicker("setDate", datebirth);
$( "#birth_date" ).datepicker("setDate", datebirth);

Edit: You could probably also do it this way, but I haven't tested it:

$( "#birth_date_display, #birth_date" ).datepicker("setDate", datebirth);

You just need to call the setDate DatePicker method. This will set the value of the DatePicker and update both text box elements with the appropriate value in the formats you specified for the dateFormat and altFormat.

var datebirth="2011-08-16"; 
$("#birth_date_display").datepicker("setDate",new Date(datebirth));

You may also try to format month names instead of integer

var Month = "Feb" //some month; 
var Year = "2013" //some year; 
var mydate = new Date('1 '+ Month + " " + Year);

$(".txtDate").datepicker('setDate', new Date(Year, mydate.getMonth(), 1));

本文标签: javascriptset or show date in textbox with jqueryui datepickerStack Overflow