Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.ParseExact function is not working as expected?

Tags:

c#

datetime

I want to parse following datetime string "2016-05-31T16:03:39.5173279Z" and receiving result is not what I would expect, I would expect that hour would be 16 not 18.

Here is a code:

string _UtcFormat= "yyyy-MM-ddTHH:mm:ss.fffffffZ";
DateTime.ParseExact("2016-05-31T16:03:39.5173279Z", _UtcFormat, new System.Globalization.CultureInfo("de-DE"))

Any comments

enter image description here

like image 749
krul Avatar asked Feb 06 '23 21:02

krul


2 Answers

You need to add DateTimeStyles.AdjustToUniversal as the last argument of DateTime.ParseExact():

DateTime dt = DateTime.ParseExact("2016-05-31T16:03:39.5173279Z", _UtcFormat, new System.Globalization.CultureInfo("de-DE"), DateTimeStyles.AdjustToUniversal);
like image 63
Roman Doskoch Avatar answered Feb 17 '23 08:02

Roman Doskoch


The bit you've perhaps not noticed is

Kind: Local

By default, ParseExact will parse the datetime and convert it to your local timezone.

If you want to ignore that, use the overload which allows you to specify DateTimeStyles - I believe the setting you want is DateTimeStyles.AdjustToUniversal

like image 24
Jamiec Avatar answered Feb 17 '23 09:02

Jamiec