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

20,018

Solution 1

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.

Solution 2

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

Solution 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));
Share:
20,018
Jarin Udom
Author by

Jarin Udom

Ruby on Rails / Adobe AIR / Adobe Flex developer specializing in eCommerce and digital media distribution. @jarinfacebook

Updated on June 08, 2020

Comments

  • Jarin Udom
    Jarin Udom almost 4 years

    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?