How to create .NET DateTime from ISO 8601

I found how to turn DateTime into ISO 8601 format , but nothing about how to do the opposite in C #.

I have 2010-08-20T15:00:00Z and I want to turn it into a DateTime object.

I could separate parts of the string myself, but this seems like a lot of work for what is already an international standard.

+76
c # datetime iso8601
Aug 24 '10 at 11:56
source share
7 answers

This solution uses the DateTimeStyles enumeration, and also works with Z.

 DateTime d2 = DateTime.Parse("2010-08-20T15:00:00Z", null, System.Globalization.DateTimeStyles.RoundtripKind); 

This perfectly displays the solution.

+81
Aug 24 '10 at 12:02
source share
 using System.Globalization; DateTime d; DateTime.TryParseExact( "2010-08-20T15:00:00", "s", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out d); 
+18
Aug 24 '10 at 12:08
source share

Although MSDN says that the "s" and "o" formats reflect the standard, they seem to be able to parse only a limited subset. This is especially a problem if the line contains a time zone specification. (Neither for basic ISO8601 formats, nor for shortened precision formats, but this is not entirely true). That is why I use custom format strings when it comes to parsing ISO8601. My current preferred snippet is:

 static readonly string[] formats = { // Basic formats "yyyyMMddTHHmmsszzz", "yyyyMMddTHHmmsszz", "yyyyMMddTHHmmssZ", // Extended formats "yyyy-MM-ddTHH:mm:sszzz", "yyyy-MM-ddTHH:mm:sszz", "yyyy-MM-ddTHH:mm:ssZ", // All of the above with reduced accuracy "yyyyMMddTHHmmzzz", "yyyyMMddTHHmmzz", "yyyyMMddTHHmmZ", "yyyy-MM-ddTHH:mmzzz", "yyyy-MM-ddTHH:mmzz", "yyyy-MM-ddTHH:mmZ", // Accuracy reduced to hours "yyyyMMddTHHzzz", "yyyyMMddTHHzz", "yyyyMMddTHHZ", "yyyy-MM-ddTHHzzz", "yyyy-MM-ddTHHzz", "yyyy-MM-ddTHHZ" }; public static DateTime ParseISO8601String ( string str ) { return DateTime.ParseExact ( str, formats, CultureInfo.InvariantCulture, DateTimeStyles.None ); } 

If you don't mind parsing TZ-less lines (I do), you can add the string "s" to significantly expand the number of format changes that have been changed.

+17
Jul 19 '13 at 17:46
source share

Here is one that works best for me ( LINQPad version):

 DateTime d; DateTime.TryParseExact( "2010-08-20T15:00:00Z", @"yyyy-MM-dd\THH:mm:ss\Z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out d); d.ToString() 

produces

 true 8/20/2010 8:00:00 AM 
+12
May 13 '11 at 16:36
source share

It seems important to exactly match the ISO string format for TryParseExact to work. I think Exact is Exact, and this answer is obvious to most, but anyway ...

In my case, the Reb.Cabin answer does not work, since I have a slightly different input according to my "value" below.

Value: 2012-08-10T14:00:00.000Z

Within a few milliseconds there are a few more 000, and there may be more.

However, if I add .fff to the format as shown below, everything is fine.

Line format: @"yyyy-MM-dd\THH:mm:ss.fff\Z"

In VS2010 Immediate window:

 DateTime.TryParseExact(value,@"yyyy-MM-dd\THH:mm:ss.fff\Z", CultureInfo.InvariantCulture,DateTimeStyles.AssumeUniversal, out d); 

true

You may need to use DateTimeStyles.AssumeLocal , and also depending on which zone your time is for ...

+3
Aug 14 '12 at 7:15
source share

This works fine in LINQPad4:

 Console.WriteLine(DateTime.Parse("2010-08-20T15:00:00Z")); Console.WriteLine(DateTime.Parse("2010-08-20T15:00:00")); Console.WriteLine(DateTime.Parse("2010-08-20 15:00:00")); 
+2
Dec 18
source share

DateTime.ParseExact(...) allows you to tell the parser what each character represents.

0
Aug 24 '10 at 12:00
source share



All Articles