tags:

views:

173

answers:

2

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...

+2  A: 

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
sorry i mean eastern time so it could be EST or EDT (Eastern Daylight Time)
Comma
it crashed when i used eastern time
Comma
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
@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
i see. it worked! thanks!
Comma
A: 

Take a look at the TimeZoneInfo class.

Philip Smith