Dealing with dates in Flex 3 is a little bit confusing and annoying to me. The latest confusion is the time zone offest functionality. If you do nothing but pass a date value from a database straight to Flex 3, that date will automatically be adjusted according to the users Time Zone.
What this means?
Say I have an application that schedules ColdFusion / Flex conferences in the United states. An admin goes in and sets the various conferences and breakout sessions times / dates. Now on the user end, a user is displayed the various conferences and breakout sessions and makes travel arrangements to attend. Here's the problem, if you do nothing with the dates stored in the database, the time zone offest will automatically change the dates to the users time zone. So the user thinks a conference starts at 12 pm but in reality it starts at 3 pm. Now let's say the conference date was saved at a default datetime of 1/19/2010 12:00AM. If a user from the west coast views that conference, the date will be 1/18/2010 10:00PM. The problem is very obvious.
Solution
I am sure there are many solutions, but this is the one that works for me. It is a simple hack that converts the timezone to a standard timezone. So when I receive a date from the database in Flex 3 I always convert the date to GMT-8. No matter what timezone you are in, the date will always be the same. It doesn't stop there, though. It is important that when you send a date back to the database that you convert it back so that it gets populated in the database correctly. Here is an example of how you can accomplish this. I use Flash Builder to create my setter and getter funcions in my ActionScript Object and apply my ActionScript DateUtil class to convert dates when needed.
public static function getTimeZoneFix(fixDate:Date, GMTHour:Number=8):Date {
var gmtDate:Date = new Date(fixDate);
var hourOffset:Number = gmtDate.getTimezoneOffset() / 60 - GMTHour;
gmtDate.setHours(gmtDate.getHours() + hourOffset );
return gmtDate;
}
public static function sendTimeZoneFix(fixDate:Date, GMTHour:Number=8):Date {
var gmtDate:Date = new Date(fixDate);
var hourOffset:Number = gmtDate.getTimezoneOffset() / 60 - GMTHour;
gmtDate.setHours(gmtDate.getHours() - hourOffset );
return gmtDate;
}
If you do not know how to auto create your ActionScript Object setter and getter functions in Flash Builder here is an example:
If you wanna read more imformation about the Flex 3 Time Zone Offset here are some valuable posts:
http://blog.shrefler.net/?p=13=1
http://www.verysimple.com/blog/2009/11/16/confusing-timezone-offset-functionality-in-flex/






Jan 20, 2010 at 10:08 AM i take it that your cf server's in UTC-8? otherwise you're going to fall into tz hell.
Jan 20, 2010 at 10:11 AM @PaulH - It is in UTC-8. Time zone hell has already been felt. Do you know of a better way?
Jan 20, 2010 at 11:05 AM better? not sure, but know lots of *different* ways ;-) mostly we try to use epoch offsets instead of datetimes (mainly trying to avoid cf's tendency to push stuff into it's own tz). is your server in a tz w/DST? better keep an eye on that. you're also going to hate flex when it comes time to deal with non-gregorian calendars...
Jan 20, 2010 at 11:29 AM @Paul H - To be honest with you I havnt dealt with epoch offsets. I have found a couple blog posts about using epoch dates just in case anyone else is interested. Check these out: http://www.coldfusioncookbook.com/entry/13/You-have-a-date,-time,-or-both-and-you-need-to-convert-it-to-Epoch-seconds. http://www.brooks-bilson.com/blogs/rob/index.cfm/2007/10/11/Some-Notes-on-Using-Epoch-Time-in-ColdFusion
Jan 20, 2010 at 11:53 AM actually to get java epoch offset all you need to do is use the getTime() function on your cf datetime object (rob uses it in his example): now=now(); epochOffset=now.getTime(); its actually undocumented but been around since cf6 or so. bit more on epochs of all sorts here: http://www.sustainablegis.com/blog/cfg11n/index.cfm?mode=entry&entry=764FA362-20ED-7DEE-2A73C18894D4D203 and tz stuff in general: http://www.sustainablegis.com/blog/cfg11n/index.cfm?mode=cat&catid=162AEDB4-20ED-7DEE-2A6CF1B79AC2E03A
Jan 20, 2010 at 11:54 AM @PaulH - Thanks for all the input and resources! Appreciate it