Friday, August 14, 2009

Time and date in XML

Today, one of my collegues was struggling with a datetime string recieved from a XML-feed. The string was on the format: "2009-08-05T19:00:00+01:00". And is called "Complete date plus hours, minutes and seconds" in the spesification on W3C.

When he tried DateTime.Parse(), the timesone was added to the time, resulting in a string like this: "05.08.2009 20:00:00".

When he tried DateTime.ParseExact(), he could not find a CultureInfo-string that matched the date format.

After some searching he found that the XML namespace also had some date functions. The result was to use the ToDateTimeOffset function in the XML namespace, like this:

System.Xml.XmlConvert.ToDateTimeOffset("2009-08-05T19:00:00+01:00").ToString("yyyy-MM-dd hh:mm s")

After even more research he came up with the final soultion:

DateTimeOffset.Parse("2003-10-29T17:44:00+01:00")


This format is often called the XML format.