I am trying to set a time to EST, and then find what it UCT time is. (We have our reasons).. I have read that "Eastern Standard Time" should take into account the Daylight savings time. But when we check the date, and we know it falls within Daylight Savings time, it still tries to convert for 5 hours instead of 4. Is there any method I am missing? Or do we have to do some manipulation.
DateTimeOffset convertDateTime = new DateTimeOffset(
subbmission.EntryDate,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").BaseUtcOffset);
I added the following bu the boss is not a fan of using this..so any ideas would be greatly appreciated.
if (zone.IsDaylightSavingTime(convertDateTime.DateTime))
{
currentDateTime = convertDateTime.DateTime.AddHours(-1);
}
else
{
currentDateTime = convertDateTime.DateTime;
}
Fundamentally, you're using the wrong approach - if you want to convert to UTC, use TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo)
. You'll want to be careful around times that are either invalid (because they were skipped) or ambiguous (because the clock went back, and the same local time happened twice).
If you actually want a DateTimeOffset
so that you can get both the local and UTC times, you could use call ConvertTimeToUtc
then compute the difference between them. It's a shame that TimeZoneInfo
doesn't appear to have a "construct a DateTimeOffset
based on this DateTime
in this time zone" but I can't see it...
As an alternative, you could use my Noda Time project which makes all of this a lot clearer, of course :)
See more on this question at Stackoverflow