10

This is seriously killing me. I'm trying to convert a Unix timestamp (1236268800, which equates to Thu, 05 Mar 2009 16:00:00 GMT) to a Date object in Flex.

var timestamp:Number = 1236268800;
trace(new Date(timestamp));

Output: Wed Jan 14 23:24:28 GMT-0800 1970

Also tried this:

var timestamp:Number = 1236268800;
var date:Date = new Date;
date.time = timestamp;
trace(date);

Output: Wed Jan 14 23:24:28 GMT-0800 1970

Either of those methods should work. What am I doing wrong here?

Jarin Udom
  • 1,849
  • 3
  • 19
  • 23

4 Answers4

21

you have to convert to milliseconds, multiply that by 1000

4

http://livedocs.adobe.com/flex/2/langref/Date.html#Date()

If you pass one argument of data type Number, the Date object is assigned a time value based on the number of milliseconds since January 1, 1970 0:00:000 GMT, as specified by the lone argument.

You need to multiply your number by 1000.

Chetan S
  • 23,637
  • 2
  • 63
  • 78
3

Since it's parsed as milliseconds, just multiply the epoch value by 1000:

trace(new Date(1236268800 * 1000));
// Thu Mar 5 08:00:00 GMT-0800 2009
Christian Nunciato
  • 10,276
  • 2
  • 35
  • 45
3

In AS3, the Date() constructor takes a value in milliseconds, whereas Unix time is in seconds. Try this:

var timestamp:Number = 1236268800;
trace(new Date(timestamp * 1000));
Rhys Causey
  • 787
  • 7
  • 21