Andreas
Andreas

Reputation: 5309

Parsing a date time from an xml document with timezone abbreviations (e.g. CET )

I rarely parse xml files and when I try to use the features integrated in Linq2Xml. However today I stumbled upon a date format I am not able to cast via:

XElement element = ...
var date = (DateTime)element;

The date is in the following format:

01/28/2009 02:31:54 CET

I already tried to parse the date using TryParse in various combinations, cultures, but whatever format I try the datetime parser complains about the space at position 20. Is there a way to parse this date without splitting the string or doing awkward things?

Upvotes: 2

Views: 1244

Answers (1)

jglouie
jglouie

Reputation: 12880

It sounds like the timezone abbreviations (e.g. CET) aren't recognized. Do you know the offset?

This person is having a related problem: Parse DateTime with time zone of form PST/CEST/UTC/etc

An example from his post:

DateTime dt1 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+2"), "dd-MMM-yy HH:mm:ss z", culture);

Obviously your date time format is a bit different, but it's an idea to bail you out.

If you can, ask the person giving you the XML to generate the date/times with ISO 8601 format to avoid custom parsing.

Upvotes: 2

Related Questions