2

I have the string "06-03-2016T06:42:44.252Z" and I would like to convert it to a datetime.

The top answer from this post suggested using:

DateTime.Parse(string,  null, System.Globalization.DateTimeStyles.RoundtripKind);

This works if I format my date like "2016-06-03T06:42:45Z" but not "06-03-2016T06:42:44.252Z"

How can I convert "06-03-2016T06:42:44.252Z" to datetime properly?

Thank you very much for your time. Please let me know if I am being unclear or if you need anything else from me.

I could not find another question on stack asking how to convert from this exact format and could not apply the strategies from them to my case. I can transform my string to match those used in the example I linked but I am losing a bit of precision and adding more work in the process. I would like to leave this question up and unmarked as a duplicate in hopes of finding a means of parsing my date format or confirming that it cannot be done.

Community
  • 1
  • 1
user95227
  • 1,853
  • 2
  • 18
  • 36
  • In case you ever want to go the other way: http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format – Cody Gray - on strike Jun 10 '16 at 17:25
  • @CodyGray I see you marked this as duplicate. The solution from that question does not work with my format. When I try that method I get a System.Format Exception. Does that suggest I need to transform my date string before trying to parse? I was hoping to find a way to parse that string because it is a valid ISO 8601 format. – user95227 Jun 10 '16 at 17:35
  • Well, yes, that is odd. That answer has a score of +50, lots of people obviously thought it was useful. It pretty clearly suggests that it will work to parse strings in ISO 8601 format. If it doesn't work for you, your quibble is not with me but with the person who posted the answer. – Cody Gray - on strike Jun 10 '16 at 17:37
  • Your string is not in ISO8601 format, the standard does not allow `.` separators and yours has one. So your title question is incorrect and this technically isn't a duplicate of that question. Anyways, this works for me: `DateTime.ParseExact("06-03-2016T06:42:44.252Z", @"MM-dd-yyyy""T""hh:mm:ss.fff""Z""", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal)`, where I just specified the format exactly, indicating that `T` and `Z` are string literals and not format specifiers. edit: added "AssumeUniversal" because the `Z` means UTC time. – Quantic Jun 10 '16 at 17:57
  • @Quantic thank you very much. I apologize for the incorrect language in the title and have removed ISO 8601 from the title. If you would like to post your code as a response I will accept it. – user95227 Jun 10 '16 at 18:26
  • 1
    We don't know if your format is `MM-dd-yyyy` or `dd-MM-yyyy`. Please clarify. – Dour High Arch Jun 10 '16 at 19:12

1 Answers1

4

Top answer you linked pointing to ISO8601 format but your string is not on that format. Since the Z in your input indicates a UTC time, I would suggest using DateTime.ParseExact, where you can specify the exact format you want and preserve the UTC time with AdjustToUniversal style:

var dt = DateTime.ParseExact(
              "06-03-2016T06:42:44.252Z",
              "MM-dd-yyyyTHH:mm:ss.fffZ",
               CultureInfo.InvariantCulture,
               DateTimeStyles.AdjustToUniversal);
Console.WriteLine(dt);      // June 03 2016 06:42 (...)
Console.WriteLine(dt.Kind); // Utc
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364