2

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?

Andreas
  • 5,251
  • 30
  • 43

1 Answers1

2

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.

Community
  • 1
  • 1
jglouie
  • 12,523
  • 6
  • 48
  • 65
  • +1 The system is a large black box, no chance to change that. Maintaining a dictionary of all possible timezone abbreviations, sounds promising :-). – Andreas Jun 12 '12 at 21:55