views:

6327

answers:

5

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?

+6  A: 

you have to convert to milliseconds, multiply that by 1000

Dan you rule so hard.
Jarin Udom
+3  A: 

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 Sastry
+2  A: 

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
+2  A: 

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
A: 

interesting... i did'nt know it

jacsdev