2

Possible Duplicate:
How to create a .NET DateTime from ISO 8601 format

How can I parse a date string in ISO 8601 format into a datetime object using C#?

Community
  • 1
  • 1
Marvin Garcia
  • 21
  • 1
  • 1
  • 2
  • 1
    Is the source string in `ISO8601` format or do you want to convert a string to that format? – Devendra D. Chavan Feb 24 '11 at 15:39
  • the source string is in ISO8601 format and I want to convert this string to that format into a datetime object. I have his :DateTime.ParseExact("2011-02-14T13:10:30", "yyyy-MM-dd'T'HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); and the result is 2011-02-14 13:10:30 and I am supposed to get 2011-02-14T13:10:30 or Am I wrong to expect this result? – Marvin Garcia Feb 24 '11 at 17:59

1 Answers1

5

Use one of the ISO 8601 standard date and time format strings - "O", "o" , "S" and "s" when parsing the string (aka the Roundtrip format specifier) .

The pattern for this specifier reflects a defined standard (ISO 8601). Therefore, it is always the same regardless of the culture used or the format provider supplied.

DateTime dt = DateTime.ParseExact(iso8601String, "s", CultureInfo.InvariantCulture);
Oded
  • 489,969
  • 99
  • 883
  • 1,009