Am working with json api that represents dates like this
"date" : "/Date(1356081900000)/"
I want to turn this into regular javascript Date.
The only way I can think of solving this problem is to do a replace on everything leaving the timestamp which I can then "convert".
This works but it just looks wrong.
My question. Can I do this in better way?
UPDATE
unix_timestamp = jsonDate.replace('/Date(', '').replace(')/', '');
newDate = new Date(+unix_timestamp + 1000*3600);
You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.
The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.
Simply multiply Unix timestamp by 1000 to convert it to a JavaScript time, because Unix timestamp measures time as a number of seconds, whereas in JavaScript time is fundamentally specified as the number of milliseconds (elapsed since January 1, 1970 at 00:00:00 UTC).
Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for Unix time.
Duplicate of How to format a JSON date?.
Accepted solution was:
var date = new Date(parseInt(jsonDate.substr(6)));
Try something like this:-
var d = new Date(unix_timestamp*1000);
or
var d = new Date([UNIX Timestamp] * 1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With