简体   繁体   中英

set or show date in textbox with jquery-ui datepicker

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:)

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM