mpma
mpma

Reputation: 35

c#: DateTime.Parse() -- Unrecognized datetime-string

So I got this datetime-string which I get from an external API, "2014-08-28T11:00:00:000-0400".

But I am unable to parse it into a normal DateTime object in C#, I know that last part of the string has something to do with timezone offsets or something, but I am at a loss as to what to do in order for it to parse.

What I've tried so far:

DateTime.Parse("2019-11-13T05:20:14:311-0700")
DateTime.Parse("2019-11-13T05:20:14:311-0700", new System.Globalization.CultureInfo("en-GB"))
DateTime.Parse("2019-11-13T05:20:14:311-0700", new System.Globalization.CultureInfo("en-US"))
DateTimeOffset.Parse("2019-11-13T05:20:14:311-0700")

A whole other bunch of stuff where I've passed in various format strings like "yyyy-MM-dd:HH:mm:ss" but that doesn't do any good either

Please, if someone out there can help me, you'll get a virtual cookie!

Upvotes: 0

Views: 788

Answers (1)

Jamiec
Jamiec

Reputation: 136074

The format string you want to use is either

yyyy-MM-dd'T'HH:mm:ss:fffzzz

or

yyyy-MM-dd'T'HH:mm:ss:fffK

More information for zzz can be found here or for K here

You can use the above format string with either DateTime.ParseExact or DateTimeOffset.ParseExact depending on what you're trying to achieve.

Live example: https://rextester.com/DTNT63275

Upvotes: 2

Related Questions