Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a .NET DateTime from ISO 8601 format

I've found how to turn a DateTime into an ISO 8601 format, but nothing on how to do the reverse in C#.

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

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

like image 428
Ripter Avatar asked Aug 24 '10 11:08

Ripter


People also ask

How do I convert ISO 8601 to date?

The date. toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss.

How do I convert ISO date to long date?

“javascript convert iso date to long date” Code Answer'sdate. getFullYear()+'-' + (date. getMonth()+1) + '-'+date.

Is ISO 8601 the same as UTC?

They're for different purposes. UTC is the primary time standard by which the world regulates clocks and time. ISO is standard format time.

What time zone is ISO 8601?

Time zones in ISO 8601 are represented as local time (with the location unspecified), as UTC, or as an offset from UTC.


1 Answers

This solution makes use of the DateTimeStyles enumeration, and it also works with Z.

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

This prints the solution perfectly.

like image 95
Mamta D Avatar answered Sep 19 '22 21:09

Mamta D