Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn a timestamp into a javascript date object? [duplicate]

Possible Duplicate:
Convert a Unix timestamp to time in Javascript

Turn a unix timestamp into 2008-07-17T09:24:17Z

How to do that?

like image 730
TIMEX Avatar asked May 01 '11 05:05

TIMEX


People also ask

How do I convert a timestamp to 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 convert a firestore date timestamp to a JS date ()?

To convert a Firestore Timestamp into a Javascript date, just call . toDate() on the Timestamp. Save this answer.

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.


2 Answers

Unix time stamp is in seconds since epoch right? You can convert it into milliseconds (by multiplying it by 1000) and passing it to the date constructor like this to convert it to a Date object.

new Date(unixtimestamp*1000)

Then you can use the Date APIs to get parts of the date.

like image 81
Raze Avatar answered Sep 19 '22 17:09

Raze


unix timestamp has an accuration of second, so convert to milisecond and pass to Date constructor:

var d = new Date(timestamp * 1000)
like image 34
otakustay Avatar answered Sep 18 '22 17:09

otakustay