Jarin Udom
Jarin Udom

Reputation: 1849

Flex Date() constructor is mis-converting Unix time stamps argh

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?

Upvotes: 10

Views: 20075

Answers (4)

Rhys Causey
Rhys Causey

Reputation: 787

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

Upvotes: 3

Christian Nunciato
Christian Nunciato

Reputation: 10409

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

Upvotes: 3

Chetan S
Chetan S

Reputation: 23813

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.

Upvotes: 4

Dan
Dan

Reputation:

you have to convert to milliseconds, multiply that by 1000

Upvotes: 21

Related Questions