Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert unix timestamp to javascript date Object [duplicate]

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);
like image 493
jamjam Avatar asked Dec 21 '12 16:12

jamjam


People also ask

How do I turn a timestamp into a date?

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.

How do I format a timestamp in JavaScript?

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.

How do I convert UNIX time to normal time?

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

Is Epoch and Unix timestamp same?

Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for Unix time.


2 Answers

Duplicate of How to format a JSON date?.

Accepted solution was:

var date = new Date(parseInt(jsonDate.substr(6)));
like image 113
antila Avatar answered Oct 08 '22 13:10

antila


Try something like this:-

 var d = new Date(unix_timestamp*1000);

or

 var d = new Date([UNIX Timestamp] * 1000);
like image 45
Rahul Tripathi Avatar answered Oct 08 '22 13:10

Rahul Tripathi