#quick snippet: UNIX Timestamp to as3 Date
The problem:
Our flashmovie fetches a Timestamp (UNIX) from some kind of backend code and you need to change this to a workable Date format. It’s actually pretty easy and almost totally standard in flash!
Note:
Flash handles time in milliseconds (think about the timerclass that expects 3000 if you want to time 3 seconds) and the UNIX timestamp is a format based on seconds from the “first day” computers started to use the Gregorian Calendar. In short: the timestamp is defined as the number of seconds elapsed since midnight Universal Time (UTC) of January 1, 1970. Your computer keeps track of how many seconds have elapsed since then and the flash Date class expects this in milliseconds. Simply multiply your seconds with 1000 and you’re done. Easy peasy!
The solution:
A quick code snippet that’s even easy to read for – let’s say – mobile developers!
// replace the capitalized variable with your unix timestamp // multiply it with 1000 (remember my note above!) var currDate:Date = new Date(Number(YOUR_TIMESTAMP_HERE)*1000); // trace out the humanly readable values trace(currDate.getDate()); trace(currDate.getMonth()); trace(currDate.getFullYear());
Done!
A real life flash example (DateDisplayer):
Download this wonderful widget here!
Go now, my young actionscript children! Feed on my example and make me proud! Soon the world will be filled with actionscript 3.0 vampires!
This entry was posted by Murten on Thursday March 25, 2010 at 12:22:38, and is filed under snippets. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site.
