1

I have questions on this date (from "Jira" REST API Result ->Worklog->Started field) where it returns different result form what I expected.

Questions:

  1. What kind/type of date format is this?
  2. Why is returning different result. String Date:2017-06-20T22:09:00.000-0400 C# DateTime.Parse Result: {6/21/2017 10:09:00 AM}

Can you please provide the correct way to convert this to its correct time?

SharpGobi
  • 144
  • 1
  • 13
mercu
  • 121
  • 2
  • 16

2 Answers2

3

It is an ISO format - the most preferable and inambiguous date format which represents a moment of time in specific time zone.

2017-06-20T22:09:00.000-0400 represents June 20, 2017 22:09 PM in time zone GMT -4.

ISO format is correctly parsed by most languages including C#.

The reason why you get another value in your code is because you are located in GMT +8 and your local time is June 21 10:09 AM when it is June 20 22:09 PM in GMT -4.
It is an absolutely valid and expected behaviour.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • Recently, we had a case where it's not parsed by c#, because in ISO 8601 `2017-06-22T24:00:00.000` is a correct date and at this point, c# is completely lost and throw an exception. – Jimbot Jun 23 '17 at 05:36
  • @Jimbot 24:00:00? Is it a leap second case? – Yeldar Kurmangaliyev Jun 23 '17 at 05:38
  • No it's perfectly ok in ISO 8601, I checked the spec, but not in c#... It just mean the next day at midnight : 1995-02-04 24:00 = 1995-02-05 00:00 – Jimbot Jun 23 '17 at 05:46
  • @Jimbot you won't find many libraries and languages that can handle 24-hour/military time. In fact, as the comment shows, many programmers don't even know about it either – Panagiotis Kanavos Jun 28 '17 at 14:52
0

Times and GMT offsets often lead to confusion.

Firstly, what does "it's 8:15am here" mean? GMT? London? Somewhere else? Secondly, what does "GMT + 2" mean?

In the first case the honest answer is there is no way to tell without a little more context. In the second case it's more down to people misunderstanding GMT offsets - many people believe that "GMT == London" so "GMT + 2 == London + 2" - of course that's not correct, "GMT == London in winter; BST (GMT + 1) == London in summer". So "GMT + 2" is "GMT + 2", i.e. somewhere like Berlin in the summer or Nicosia in the winter.

For these reasons many situations where time is important use GMT or another timezone but clearly state the offset, e.g. "3:15am, EDT".

To answer your question you have a few options

  • Allow for the timezone in the things you're doing; maybe changing it to GMT to make things easier - you'd then be doing GMT +/- rather than GMT - 4 +/-.
  • Alternatively, you could do something along the same lines as the answer to this question.

Something like this

DateTimeOffset date = new DateTimeOffset(2017, 6, 20, 22, 09, 0, 0, TimeSpan.FromHours(-4));
// 20 June 2017, 22:09, GMT-4

public static DateTimeOffset ParseIso8601(string iso8601String)
{
    return DateTimeOffset.ParseExact(
        iso8601String,
        new string[] { "yyyy-MM-dd'T'HH:mm:ss.FFFK" },
        CultureInfo.InvariantCulture,
        DateTimeStyles.None);
}
Gareth
  • 913
  • 6
  • 14