13

Using C#, I need to convert incoming datetime values into UTC. I know there is functionality in .NET for these conversions but all I have to identify the timezone is the standard timezone list

http://www.timegenie.com/timezones

What's the best way to do this in .NET? Will I need to create a mapping table to convert the timezones to the IDs in TimeZoneInfo.GetSystemTimeZones() (e.g. "Pacific Standard Time (Mexico)") so that I can then use TimeZoneInfo.FindSystemTimeZoneById()?

Thanks for any help

Bobbler
  • 633
  • 1
  • 7
  • 21
  • 1
    How will you know the incoming values had as a Timezone? – Alastair Pitts Aug 04 '11 at 23:27
  • So your input is a `DateTime` and a UTC-offset? Then take a look at the [DateTimeOffset Structure](http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx). Otherwise, can you give an example of your input and the desired output? – dtb Aug 04 '11 at 23:27
  • How do you want to figure out whether AST is Arabic Standard Time (UTC+3) or Atlantic Standard Time (UTC-4)? The table is going to have several other ambiguities. – svick Aug 04 '11 at 23:33
  • This question http://stackoverflow.com/questions/4653541/modifying-dates-in-asp-net-mvc-c-based-on-stored-time-zone/6826513#6826513 is not exactly what you have asked. But this will give good guidance on implementation. – Jeyara Aug 04 '11 at 23:35
  • @svick: You don't store the timezones in the database using ambiguous names like AST. You use their Id from a call to GetSystemTimeZones, which in those cases gives you 'Arabic Standard Time' and 'Atlantic Standard Time'. – Bennor McCarthy Aug 04 '11 at 23:42
  • @Bennor, yeah, but I was assuming the incoming dates are going to use the abbreviations. – svick Aug 04 '11 at 23:47
  • Well, of course not. He's getting a timestamp without any timezone indication and without a clue where it came from. There wouldn't be any point in asking the question otherwise. There still isn't. – Hans Passant Aug 05 '11 at 00:38

3 Answers3

18

I've done it before by storing the timezone id in the database using a mapping table. i.e. A table containing the results of TimeZone.GetSystemTimeZones()

You don't actually need to use TimeZoneInfo.FindSystemTimeZoneById() though: you can do the conversion using one of the overloads of TimeZoneInfo.ConvertTimeBySystemTimeZoneId(). This method has some overloads which take DateTime values, and some that take DateTimeOffset values (which are preferable as they specify an unambiguous point in time).

e.g.

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "New Zealand Standard Time", "UTC")

The real benefit of using the system time zone id rather than an offset stored in the database is that daylight savings time is automatically handled for you by TimeZoneInfo.ConvertTimeBySystemTimeZoneId.

Bennor McCarthy
  • 11,415
  • 1
  • 49
  • 51
3

DateTime.ToUniversalTime Method

Or

DateTime.UtcNow Property

Peyman
  • 3,068
  • 1
  • 18
  • 32
2

There is a lot of information on MSDN about converting to different time zones.

You probably want to use ConvertTimeBySystemTimeZoneId method (follow link for examples).

Dennis
  • 20,275
  • 4
  • 64
  • 80