Jan/102
Unix Timestamp in Flash
Recently i needed to convert normal Time to Unix Timestamp in Flash. I couldn't get how to go about it intially. But when i gone through the definition of Unix Timestamp, its was clear for me.
"The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch."
In order to count the number of seconds till a time, we have method Date.getTime() in Flash. This will effectively return the number of seconds from the Unix Epoch till the Date.
This is how i tried to find out the number of seconds till today's System Time:
var myDate = new Date();
trace(myDate);
var unixTime = Math.round(myDate.getTime()/1000);
trace("Unix Time: "+unixTime);
Output:
Fri Jan 22 13:01:55 GMT+0530 2010 Unix Time: 1264145516
To verify if we got the correct Unix TimeStamp of the normal time, check it on any online Unix Timestamp converters.
Problem arised when tried to convert the Unix Time we got into normal timestamp in Flash.
var myDate = new Date();
trace(myDate);
var unixTime = Math.round(myDate.getTime()/1000);
trace("Unix Time: "+unixTime);
var dateNew = new Date();
dateNew.setTime(unixTime);
trace(dateNew);
Output:
Fri Jan 22 16:22:42 GMT+0530 2010 Unix Time: 1264157563 Thu Jan 15 20:39:17 GMT+0530 1970
If you observe closely, while converting the Unix Timestamp to normal time, the year that's returned is 1970. If we use a online Unix Timestamp Converter to get back the normal time, it returns approximately correct time by inputting the above Unix Timestamp.
That was strange to get the difference while converting Unix Timestamp to Normal TimeStamp. Just observed this behavior, so thought of sharing it to find a solution or answer how to overcome it.
Recent Comments
Archives
- April 2010 (3)
- March 2010 (2)
- February 2010 (3)
- January 2010 (1)
- December 2009 (15)
- November 2009 (24)
- October 2009 (22)
- September 2009 (24)
- August 2009 (3)
Categories
- Adobe (6)
- Adobe Dreamweaver (1)
- Adobe Illustrator (1)
- Tutorials (1)
- AIR (13)
- Apple (4)
- Applications (2)
- IPhone (1)
- CSS (4)
- Flash (41)
- FL for Digital Home (6)
- Flash Components (2)
- FlashLite (5)
- News (1)
- Websites (2)
- Flash Catalyst (2)
- Flex (9)
- Google (2)
- Icons (1)
- Microsoft (1)
- Photoshop (15)
- Security (1)
- Wordpress (2)
Calender
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Apr | ||||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | |||
February 25th, 2010
The explanation about why the timestamp differs if you re-enter it is because Flash has a different approach to epoch’s: not in seconds but in milliseconds. While you’re dividing by 1000, you’re not multiplying it back again..
Try this instead:
var dateNew = new Date();
dateNew.setTime(unixTime*1000);
trace(dateNew);
February 25th, 2010
Thanks Eric for the suggestion. It works fine now…