TimezoneInfo and DateTime

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) Arizona

you 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
  1. TimeZoneInfo timeZoneInfo;
  2. DateTime testdate = DateTime.Parse(“3/2/2011 1:00:00 AM”);
  3. DateTimeOffset offset;
  4. DateTime utcdate = testdate.ToUniversalTime();
  5. //Set the time zone information to US Mountain Standard Time -arizona
  6. timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“US Mountain Standard Time”);
  7. //Get date and time in US Mountain Standard Time
  8. DateTime dateTime = TimeZoneInfo.ConvertTime(testdate, timeZoneInfo);
  9. //Print out the date and time
  10. Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
  11. //Mountain Standard Time
  12. timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“Mountain Standard Time”);
  13. //Get date and time in US Mountain Standard Time
  14. dateTime = TimeZoneInfo.ConvertTime(testdate, timeZoneInfo);
  15. ////Print out the date and time
  16. Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
  17. //==============================================================================================
  18. //Set the time zone information to US Mountain Standard Time -est
  19. timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“Eastern Standard Time”);
  20. //Get date and time in US Mountain Standard Time
  21. dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
  22. //Print out the date and time
  23. Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
  24. //================================================================================================
  25. //Set the time zone information to US Mountain Standard Time -cst
  26. timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“Central Standard Time”);
  27. //Get date and time in US Mountain Standard Time
  28. dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
  29. //Print out the date and time
  30. Console.WriteLine(dateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
  31. //================================================================================================
  32. //Convert it back to UTC
  33. DateTime utcDateTime = dateTime.ToUniversalTime();
  34. //Print out the date and time
  35. Console.WriteLine(utcDateTime.ToString(“yyyy-MM-dd HH-mm-ss t\\M”));
  36. //==============================================================================================================================
  37. Datetime newOffsetDate = testdate.Add(timeZoneInfo.BaseUtcOffset);
  38. 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.

Advertisements