18

Possible Duplicate:
Convert a Unix timestamp to time in Javascript

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

How to do that?

Community
  • 1
  • 1
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

3 Answers3

35

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.

Jonik
  • 80,077
  • 70
  • 264
  • 372
Raze
  • 2,175
  • 14
  • 30
  • 5
    To answer the exact original question ("unix timestamp into 2008-07-17T09:24:17Z"): Use [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) to turn Unix timestamp into an ISO 8601 string: `new Date(unixtimestamp * 1000).toISOString()` – Jonik Jan 14 '15 at 09:58
13

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

var d = new Date(timestamp * 1000)
otakustay
  • 11,817
  • 4
  • 39
  • 43
0

As long as it's a valid date string, you should be able to get a Date object out of it using Date.parse().

Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66