Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't DateTime.Parse parse UTC date

Why can't it parse this:

DateTime.Parse("Tue, 1 Jan 2008 00:00:00 UTC") 
like image 593
David Avatar asked Nov 18 '09 15:11

David


People also ask

How do you convert DateTime to UTC?

To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method. If the date and time instance value is an ambiguous time, this method assumes that it is a standard time.

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 09:05:16Z. Note that the Z letter without a space.

Is C# DateTime UTC?

The DateTime. SpecifyKind() method in C# is used to create a new DateTime object that has the same number of ticks as the specified DateTime but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value.


1 Answers

It can't parse that string because "UTC" is not a valid time zone designator.

UTC time is denoted by adding a 'Z' to the end of the time string, so your parsing code should look like this:

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

From the Wikipedia article on ISO 8601

If the time is in UTC, add a 'Z' directly after the time without a space. 'Z' is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".

UTC time is also known as 'Zulu' time, since 'Zulu' is the NATO phonetic alphabet word for 'Z'.

like image 125
Simon P Stevens Avatar answered Sep 22 '22 19:09

Simon P Stevens