JQuery UI Datepicker will not accept unix timestamp as default value

I have a Unix timestamp "1264529457" that translates to January 26, 2010, which is stored inside an input element named America.

When initializing jQuery UI Datepicker, I have the following code to set the default date:

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

When I manually verify that this is happening 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 assume I missed something)?

+4
source share
3 answers

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

+10
source

Ben is right, you need to multiply the timestamp by 1000 since Javascript uses miliseconds. That should work.

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

+1
source

You can simply pass this value to the Date() constructor as a number, for example:

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

Here you can check it out .

0
source

Source: https://habr.com/ru/post/1333138/


All Articles