最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - set or show date in textbox with jquery-ui datepicker - Stack Overflow

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论