Flex 3 Date Time Zone Issue and Fix

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:

[memento:FlashBuilderGetterSetterExample]

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/

I have done another post on using Epoch Dates.  CHeck it out http://cftips.net/post.cfm/flex-time-zone-management

 

del.icio.us Digg StumbleUpon Facebook Google Bookmarks DZone
| View count: 6621
blog comments powered by Disqus
metre