Convert UTC to Eastern timezone

c#, timezone

Solution

I will try to dissect your question, but next time please show some code so everyone can see exactly what you mean more clearly.

I have a UTC timestamp coming in UTC (string type).

Ok, I assume you mean something like this:

string utcString = "2014-02-25T12:34:56.000Z";

This is an ISO-8601 UTC timestamp. If it's in some other format, let me know in comments and I will update the answer accordingly.

... The problem is when I call `DateTime.Parse` or `Convert.ToDateTime` on the UTC timestamp, it is converting it to my local time, which is Central Time.

DateTime utcDateTime = DateTime.Parse(utcString,
                                      CultureInfo.InvariantCulture,
                                      DateTimeStyles.RoundtripKind);

The `RoundtripKind` style tells the parser to look for "kind" information in the input string, such as the `Z` that indicates UTC. The resulting `DateTime` will have the original value, and it's `.Kind` property set to `DateTimeKind.Utc`.

... I want this timestamp to be converted to Eastern Time.

Now that you have a UTC `DateTime`, you can convert it easily using the `TimeZoneInfo` class.

TimeZoneInfo easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById(
                                                         "Eastern Standard Time");

DateTime easternDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime,
                                                           easternTimeZone);

That's it. The result is a `DateTime` containing the local time in the US Eastern time zone. It's kind will be `Unspecified`, as the time zone information is not carried along with the `DateTime` object.

Also note that "Eastern Standard Time" refers to the entire US Eastern time zone, inclusive of both EST and EDT.

Problem

I have a UTC timestamp coming in UTC (string type). I want this timestamp to be converted to Eastern Time. The problem is when i call DateTime.Parse or Convert.ToDateTime on the UTC timestamp, it is converting it to my local time, which is Central Time. How can I take a string timestamp and convert it to Eastern Time regardless of the local time of the server its running on?

Original source