5

How do I convert a DateTime from EST/EDT to GMT but I don't know where the code will be ran (unknown local time zone) and also account for time savings...

Comma
  • 1,567
  • 3
  • 15
  • 22
  • Related question - http://stackoverflow.com/questions/2532729/daylight-saving-time-and-timezone-best-practices – Oded Jul 26 '10 at 19:13

2 Answers2

14

You want TimeZoneInfo.ConvertTimeToUtc(), which allows you to pass the source time zone info as a parameter. For example:

TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime someDateTimeInUtc = TimeZoneInfo.ConvertTimeToUtc(someDateTime, est);

I think this will automatically handle daylight-saving time, but you'll want to test it to be sure.

JaredReisinger
  • 6,955
  • 1
  • 22
  • 21
  • 1
    sorry i mean eastern time so it could be EST or EDT (Eastern Daylight Time) – Comma Jul 23 '10 at 23:56
  • it crashed when i used eastern time – Comma Jul 24 '10 at 00:11
  • Yup... if you look at the docs, and then look in your registry (where the FindSystemTimeZoneInfoById() does its lookup), you'll see that the "Eastern Standard Time" key has a "Dynamic DST" subkey... so *hopefully* the ConvertTimeToUtc() call will make use of it as needed. – JaredReisinger Jul 24 '10 at 00:15
  • @Comma... "it crashed when i used eastern time"... do you mean you passed "eastern time" to the FindSystemTimeZoneById() method? You should pass "Eastern Standard Time", even though it will sometimes be a daylight-saving-time time. – JaredReisinger Jul 24 '10 at 00:16
  • Microsoft, Why is the timezone called Eastern Standard Time, when Eastern Standard Time is the non-DST version of the all-year name "Eastern Time"? Microsoft fucked up the naming here. so stupid and noone knows how it works because they used the wrong name. blows the mind. – Luke Jul 08 '23 at 22:46
0

Take a look at the TimeZoneInfo class.

Philip Smith
  • 2,741
  • 25
  • 32