Apply DateTime offset to US Eastern Timezone

13,000

Solution 1

Check out the TimeZoneInfo class. It has methods to get UTC offsets for any timezone.

http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx

Also, check out this previous post: How to use TimeZoneInfo to get local time during Daylight Savings Time?

Here's an example showing a conversion from Pacfic to Eastern:

var eastern = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var local = TimeZoneInfo.Local; // PDT for me
Console.WriteLine(DateTime.Now.Add(eastern.BaseUtcOffset - local.BaseUtcOffset));

Shows 4/4/2011 12:05:31 when my local clock shows 9:05:31.

Solution 2

You can use TimeZoneInfo Class.

Take a look at this sample:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
TimeSpan offset = tzi.GetUtcOffset(DateTime.Now);

DateTime testDateTime = DateTime.Now.Add(offset);
Share:
13,000

Related videos on Youtube

Mark Ursino
Author by

Mark Ursino

...nothing to see here, move along...

Updated on June 04, 2022

Comments

  • Mark Ursino
    Mark Ursino almost 2 years

    I have a page that generates vCal and iCal files to import events into Outlook and iCal. These mail clients (Outlook & iCal) will take in the dates in UTC since they know the user's offset and will handle putting in the right times. Our client for which we're writing this code is based out of the USA Eastern time zone and so am I. Their host is in the Central time zone (1 hour behind Eastern). I'd like to make the code handle this without any hard-coded offsets. My code currently get's the DateTime from our CMS and converts to UTC for Outlook/iCal. That means the DateTime value from the CMS is relative to the hosting location:

    // Start Date Time
    sb.AppendFormat("DTSTART:{0}\n", thisEvent.StartDate.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
    

    When I run this on my local, it's all fine, because the DateTime in the CMS is based on our client's Eastern time zone and so am I, so the process is Eastern -> UTC -> into Outlook which then goes UTC -> Eastern, and everything is good. When I deploy to the server in Central, the DateTime from the CMS is an hour off from Eastern. How can I automatically get the offset from Eastern and add it in before I convert to UTC? I obviously need this to handle both standard time (EST) and daylight time (EDT). I'd like to do it programatically without hard-coded values so it always works correctly no matter what time zone you're in. I.e. if I deliver this code to someone for development in India, it should automatically handle their local server's offset and adjust accordingly.

    I need to do something like this where I apply an offset to Eastern time before converting to UTC:

    sb.AppendFormat("DTSTART:{0}\n", thisEvent.StartDate.ApplyEasternOffset().ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
    

    Sorry if this is a topic that's been discussed before, I'm just not sure of the best terms to find a question like this. Thanks in advance.

  • Mark Ursino
    Mark Ursino about 13 years
    This helped me solve the issue. It turns out I needed to add the Eastern offset AFTER converting to UTC because the ToUniversalTime() method already handled it from the local TZ. So I changed it to: TimeSpan easternOffset = TimeZoneInfo.Local.BaseUtcOffset - easternTZ.BaseUtcOffset; and thisEvent.StartDate.ToUniversalTime().Add(easternOffset)

Related