0

I am attempting to convert this string time value 2017-01-10T13:19:00-07:00 to local time (Eastern). Now from my research 07:00 is Mountain Time which is 2 hours beind Eastern Time (my local). However, when I run this syntax to convert the returned output is 01/17/2017 10:19:00 AM which is 3 hours difference, not 2.

This is my syntax I am using, is this set-up incorrectly? What should I change in order to have the accurate local time returned from the UTC time?

static void Main(string[] args)
{
    string green = "2017-01-10T13:19:00-07:00";

    DateTime iKnowThisIsUtc = Convert.ToDateTime(green);
    DateTime runtimeKnowsThisIsUtc = DateTime.SpecifyKind(
        iKnowThisIsUtc,
        DateTimeKind.Utc);
    DateTime localVersion = runtimeKnowsThisIsUtc.ToLocalTime();
    Console.WriteLine(localVersion);
    Console.ReadKey();
}

EDIT
I have verified my computer is set to the correct time zone by using the following syntax which produces Eastern for both (which is correct)

TimeZone zone = TimeZone.CurrentTimeZone;
string standard = zone.StandardName;
string daylight = zone.DaylightName;
Console.WriteLine(standard);
Console.WriteLine(daylight);
  • 1
    I don't know the answers to this but I do know that you should probably work on how to name your variables properly ;p – Markinson Jan 20 '17 at 14:07
  • @Markinson - this is just example. When I get it working like i want the variables will have meaningful names as they are supposed to. – StarsFlyFree FromCozyNights Jan 20 '17 at 14:08
  • Have you verified that your local time is actually set to eastern time, and daylight savings is set or not set appropriately? – TJ Rockefeller Jan 20 '17 at 14:15
  • @TJRockefeller - yes I have. See my edit for how I verified. – StarsFlyFree FromCozyNights Jan 20 '17 at 14:21
  • The *only* way you can be certain is to use `DateTimeOffset` with an explicit offset. – Panagiotis Kanavos Jan 20 '17 at 14:29
  • It's not due to MDT vs MST? Does eastern time have daylight savings? Check out NodaTime too. – Mike Miller Jan 20 '17 at 14:35
  • I just noticed that according to your values listed something is more wrong than you think. The time returned for eastern time should be two hours later, not 3 hours earlier, so you are actually off from the correct time by a lot more than you originally thought. – TJ Rockefeller Jan 20 '17 at 14:40
  • `iKnowThisIsUtc` is a dreadfully poorly named variable. Convert.ToDateTime() produces local time, not UTC. Easy to see by looking at the Kind property with the debugger. So name it `iKnowThisIsLocal` and you'll now know to delete the next two buggy statements. – Hans Passant Jan 20 '17 at 14:51

3 Answers3

1

Convert the string to a DateTime object:

var datetime = DateTime.Parse("2017-01-10T13:19:00-07:00");

Get the timezone for EST:

var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

Convert to EST (note the conversion .ToUniversalTime()):

var easternTime = TimeZoneInfo.ConvertTimeFromUtc(datetime.ToUniversalTime(), easternZone);

Output of easternTime.ToString();:

10/01/2017 15:19:00

(I'm in the UK hence dd/MM/yyyy, yours may show differently)

Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • This works, but it's not ideal. Parsing with offset to `DateTime` switches the `Kind` to `DateTimeKind.Local`. Then later you use the `ToUniversalTime` function to convert from local to UTC. Really the local machine's time zone should be left out of the equation. Also worth noting that this *will* survive DST fall-back ambiguities, but only due to [the hidden fourth `DateTimeKind`](https://codeblog.jonskeet.uk/2012/05/02/more-fun-with-datetime/). A cleaner way is with just `DateTimeOffset`, as I show in my answer. But thanks. :) – Matt Johnson-Pint Jan 20 '17 at 21:57
1
// your input string
string green = "2017-01-10T13:19:00-07:00";

// parse to a DateTimeOffset
DateTimeOffset dto = DateTimeOffset.Parse(green);

// find the time zone that you are interested in.
// note that this one is US Eastern time - inclusive of both EST and EDT, despite the name.
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

// convert the DateTimeOffset to the time zone
DateTimeOffset eastern = TimeZoneInfo.ConvertTime(dto, tzi);

// If you need it, you can get just the DateTime portion.  (Its .Kind will be Unspecified)
DateTime dtEastern = eastern.DateTime;
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

When your application needs to be unambiguously aware of timezones, you may want to consider a DateTimeOffset instead of DateTime.

Choosing Between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo

And this question looks like someone has collected lots of best practices on this topoic - Daylight saving time and time zone best practices

Community
  • 1
  • 1
Michael Levy
  • 13,097
  • 15
  • 66
  • 100