Until recently what used to be a headache for most of us is finally put out of misery by MS after framework 3.5.Yes there is code to finally get the datetime based on a given timezone and back as even i stumbled upon it a few months back. For eg if you have concerned about the various timezones and their datetimes in US which has several as follows.
//Eastern Standard Time (GMT-05:00) Eastern Time (US & Canada)
//Central Standard Time (GMT-06:00) Central Time (US & Canada)
//Mountain Standard Time (GMT-07:00) Mountain Time (US & Canada)
//Pacific Standard Time (GMT-08:00) Pacific Time (US & Canada)
//Alaskan Standard Time (GMT-09:00) Alaska
//Hawaiian Standard Time (GMT-10:00) Hawaii
//US Mountain Standard Time (GMT-07:00) Arizonayou can infact convert a DateTime to any other datetime in the world by passing in the timezone they are located in and then you can convert them to UTC or to local and vice versa.Lots of interesting stuff that we can really make our hands dirty.All thanks to the new TimezoneInfo class that has been introduced.You can even get the TimeZoneInfo.BaseUtcOffset of a given location and then find out the difference between that place and local time or UTC for that matter.You can even add datetime offset to the UTC date and get the datetime of a given place.
Let look at an easy example of how we achieve this.
Code Snippet
- TimeZoneInfo timeZoneInfo;
- DateTime testdate = DateTime.Parse(“3/2/2011 1:00:00 AM”);
- DateTimeOffset offset;
- DateTime utcdate = testdate.ToUniversalTime();
- //Set the time zone information to US Mountain Standard Time -arizona
- timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“US Mountain Standard Time”);
- //Get date and time in US Mountain Standard Time
- DateTime dateTime = TimeZoneInfo.ConvertTime(testdate, timeZoneInfo);
- //Print out the date and time
- Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
- //Mountain Standard Time
- timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“Mountain Standard Time”);
- //Get date and time in US Mountain Standard Time
- dateTime = TimeZoneInfo.ConvertTime(testdate, timeZoneInfo);
- ////Print out the date and time
- Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
- //==============================================================================================
- //Set the time zone information to US Mountain Standard Time -est
- timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“Eastern Standard Time”);
- //Get date and time in US Mountain Standard Time
- dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
- //Print out the date and time
- Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
- //================================================================================================
- //Set the time zone information to US Mountain Standard Time -cst
- timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“Central Standard Time”);
- //Get date and time in US Mountain Standard Time
- dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
- //Print out the date and time
- Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
- //================================================================================================
- //Convert it back to UTC
- DateTime utcDateTime = dateTime.ToUniversalTime();
- //Print out the date and time
- Console.WriteLine(utcDateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
- //==============================================================================================================================
- Datetime newOffsetDate = testdate.Add(timeZoneInfo.BaseUtcOffset);
- Console.WriteLine(utcDateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
In case if you are wondering if there is support for DST,yes there is!I just made a sample app for you guys to play around and tinker with.(currently it has only US timezones) Click me to Download!
**Note—Always use UTC as the datetime to be stored in DB.