Why DateTime.Parse does not handle UTC date

Why can't he make it out:

DateTime.Parse("Tue, 1 Jan 2008 00:00:00 UTC") 
+33
c # datetime parsing
Nov 18 '09 at 15:06
source share
9 answers

This line cannot be parsed because "UTC" is not a valid time zone pointer.

UTC time is indicated by adding "Z" to the end of the time line, so your parsing code should look like this:

 DateTime.Parse("Tue, 1 Jan 2008 00:00:00Z"); 

From Wikipedia article on ISO 8601

If the time is in UTC, add 'Z' immediately after the time without space. "Z" is the zone designation for the UTC zero offset. β€œ09:30 UTC” is therefore represented as β€œ09: 30Z” or β€œ0930Z”. β€œ14:45:15 UTC” will be β€œ14: 45: 15Z” or β€œ144515Z”.

UTC is also known as Zulu, because Zulu is the phonetic alphabetical word for Z.

+54
Nov 18 '09 at 15:12
source share
β€” -

You need to specify the format:

 DateTime date = DateTime.ParseExact( "Tue, 1 Jan 2008 00:00:00 UTC", "ddd, d MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture); 
+13
Nov 18 '09 at 15:15
source share

or use the SetToUniversal DateTimeStyle parameter in the call

 DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 
+9
Nov 18 '09 at 15:15
source share

Assuming you use the β€œo” format for your date-time, so you have β€œ2016-07-24T18: 47: 36Z”, there is a very simple way to handle this.

Call DateTime.Parse("2016-07-24T18:47:36Z").ToUniversalTime() .

What happens when you call DateTime.Parse("2016-07-24T18:47:36Z") , you get the DateTime value set to the local time zone. Thus, it converts it to local time.

ToUniversalTime() changes it to a UTC DateTime and converts it back to UTC.

+8
Jul 24 '16 at 19:28
source share

This is not a valid format, however, "Tue, 1 Jan 2008 00:00:00 GMT" is.

The documentation is written as follows:

A string that includes time zone information that complies with ISO 8601. For example, the first of the following two lines indicates Coordinated Universal Time (UTC); the second indicates the time in the time zone seven hours earlier than UTC:

2008-11-01T19: 35: 00.0000000Z

A string that includes the GMT symbol and corresponds to the temporary format RFC 1123. For example:

Sat, 01 Nov 2008 19:35:00 GMT

A string that includes the date and time along with time zone offset information. For example:

01/03/2009 05:42:00 -5: 00

+6
Nov 18 '09 at 15:15
source share

To correctly parse the line specified in the question without changing it, use the following:

 using System.Globalization; string dateString = "Tue, 1 Jan 2008 00:00:00 UTC"; DateTime parsedDate = DateTime.ParseExact(dateString, "ddd, d MMM yyyy hh:mm:ss UTC", CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal); 

This implementation uses a string to indicate the exact format of the date string that is being processed. The DateTimeStyles parameter is used to indicate that this string is a coordinated universal time string.

+5
May 29 '14 at 2:03
source share

Just use this:

 var myDateUtc = DateTime.SpecifyKind(DateTime.Parse("Tue, 1 Jan 2008 00:00:00"), DateTimeKind.Utc); if (myDateUtc.Kind == DateTimeKind.Utc) { Console.WriteLine("Yes. I am UTC!"); } 

You can test this code with the C # online compiler:

http://rextester.com/

Hope this helps.

+1
Mar 07 '17 at 4:05
source share

Just replace β€œUTC” with β€œGMT” - a simple one and don’t break correctly formatted dates:

 DateTime.Parse("Tue, 1 Jan 2008 00:00:00 UTC".Replace("UTC", "GMT")) 
0
Mar 16 '17 at 16:42
source share

Not sure why, but you can transfer DateTime.ToUniversalTime to try / catch and achieve the same result in more code.

Good luck.

-3
Nov 18 '09 at 15:08
source share



All Articles