Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string "yyyy-MM-ddZ" to a date time with .net?

I am having problems converting a string in the format "yyyy-MM-ddZ" using VB.net.

For example I have the string "2007-10-21Z".

Using CDate or TryParse it comes out to 10/20/2007 instead of 10/21/2007.

I'm not understanding how the Z affects the date string so that when it is parsed it results in the day before.

From what I understand Z specifies the zero timezone. But the date string has no time information. How does this work?

like image 271
dtc Avatar asked Dec 02 '22 09:12

dtc


2 Answers

It's interpreting the date as midnight Zulu (GMT) time and then converting it back to your local time zone. If you're in the States that would be between 3:00PM to 7:00 PM in the previous day.

like image 77
Turnkey Avatar answered Dec 28 '22 12:12

Turnkey


Try

DateTime.ParseExact("2007-10-21Z", "yyyy-MM-ddZ", CultureInfo.InvariantCulture);

like image 35
tdavisjr Avatar answered Dec 28 '22 12:12

tdavisjr