开发者

jQuery UI Datepicker won't accept unix timestamp as default value

I have the Unix timestamp "1264529457", which translates into January 26, 2010, which is stored inside an input element named "america".

When initializing jQuery UI's 开发者_运维问答Datepicker, I have the following code to set the default date:

defaultDate: $.datepicker.parseDate('@', $("input[name=america]").val()),

When I manually check to see what this comes out to in Firebug, it says "Thu Jan 15 1970 00:00:00 GMT-0500 (Eastern Standard Time) {}". Any idea what is wrong (the documentation for this function is a bit sparse, so I'm guessing I missed something)?


Unix time is stored in seconds while Javascript uses milliseconds. Try multiplying your Unix timestamp by 1000 first.


Ben is correct, you need to multiply the timestamp by 1000 as Javascript uses miliseconds. This should work.

defaultDate: $.datepicker.parseDate('@', $("input[name=america]").val()*1000),


You can just pass that value to the Date() constructor as a number, like this:

$("#datepicker").datepicker({
    defaultDate: new Date(parseInt($("input[name=america]").val(), 10))
});

You can test it out here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜