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,477
  • 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

12

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,665
  • 1
  • 21
  • 20
  • 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
0

Take a look at the TimeZoneInfo class.

Philip Smith
  • 2,641
  • 24
  • 32