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

4

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?

Empson answered 13/2, 2009 at 0:17 Comment(0)
S
21

you have to convert to milliseconds, multiply that by 1000

Slimsy answered 13/2, 2009 at 0:31 Comment(0)
F
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.

Fishhook answered 13/2, 2009 at 0:34 Comment(0)
I
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
Inelegancy answered 13/2, 2009 at 0:34 Comment(0)
M
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));
Moskow answered 13/2, 2009 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.