DIGG IT!
Published
Friday, October 01, 2004
at
10:43 AM
.
Working with time can be very frustrating. Here are some pointers that simplify working with time in Flash allowing you to work effectively with GMT.&
1. Use date and time as a GMT number and use the Date Object for conversion.
Why? Working with Date objects can be frustrating and most of the time, using a numerical equivalent is much easier and much cleaner especially when adding and subtracting time.
currentGMT = new Date().getTime()
trace(currentGMT) // 1096649692484 or Fri Oct 1 12:54:52 GMT-0400 2004
The value 1096649692484 is the milliseconds from Jan 1, 1970 in universal time according to your system time and time zone. Universal time or Zulu Time or Greenwich Mean Time(GMT) is the time at the Royal Greenwich Observatory near London, England. When working within the Date Object, internally it works with GMT and adjusts the output for your local time zone. All values returned from a date object include adjustments for the local time zone, where the getTime value is the real GMT value in Milliseconds from 1970. This value is time zone distortion free.
Subtract 2 hours from Fri Oct 1 12:54:52 GMT-0400 2004:
How many Millseconds in 1 hour from Google Calculator
trace( new Date( new Date( 1096649692484 ) - new Date( 3600000 * 2)) )
or
trace( new Date( 1096649692484 - 3600000 * 2 ))
Date objects are converted to a GMT number before math operations. When you get a number as a result of adding 2 date objects, the result is always GMT milliseconds. If your not prepared for this it will throw you for a loop.
2. Always store time and date as GMT.
GMT allows you to localize time in your applications. If you store time as a combination of date, hour, minutes, seconds and millisecond, you will have neglected the time zone. This is especially important when you store values into a database where users will be viewing data based on times entered. If the application adding times into the database is entering local time, each value is distorted according to the values of the time zone where the data was entered. It is ideal to make time comparable so if a user is in Guong Zho, China or in San Francisco the times will be comparable even though the users are on wildly different time zones. GMT allows everyone to speak the same time language.
3. Convert GMT to Date within Date constructor
The Date object in Flash supports a constructor argument allowing you to create a Date object set to a GMT time zone instantly.
myTime = new Date (1096649692484)
trace(myTime) //Fri Oct 1 12:54:52 GMT-0400 2004
Next time you do a project involving time, remember GMT is your friend.
Cheers,
ted ;)
Good one. Recently I was working with time in flash. I needed to build an an array for the dataProvider of a comboBox. I needed to have a drop down list of the last four weeks, with their weeks starting on Friday. The return value would be in the ISO 8601 format. So yesterday the dropdown would have the values:
2004-09-24, 2004-09-30 (stops on the current date)
2004-09-17, 2004-09-24
etc
I could not find a way to do this in flash so I made a PHP service to do this. PHP has a very nice method for handling Date. I may try and port that over to Flash now.
Great article
which reduces the code very much
how would one approach the day light saving aspect based on this approach
and identify a users time zone if possible ?
Regards
RD
(given "comment" 3 above this, I'm not sure you'll see this...)
I'm learning Flex. I have lots of computer software design background.
I'm working with Flex Builder 3 Beta.
I'm filling up a GridColumn with timestamp data that came from Java and is now in XML/string form.
"2008-01-09 15:41:40.0"
I'm looking for how to write and install a custom formatter or renderer (not sure which) to customize the output to something like
"2 days ago"
"7 hours ago"
"5 minutes ago"
etc.
If there's any direction you can point me, I'd appreciate it!