开发者

set or show date in textbox with jquery-ui datepicker

开发者 https://www.devze.com 2023-03-28 13:49 出处:网络
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.

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",
 altForma开发者_C百科t: "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));

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号