60

Obviously the reader doesn't like this format incoming from the response XML.

Wondering if I can reformat this. Trying to convert to DateTime using the following code with my XmlReader:

reader.ReadContentAsDateTime();
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
user72603
  • 937
  • 4
  • 12
  • 18

2 Answers2

120

According to the XML schema spec, date time values should be in ISO8601 format, e.g., something like

2009-03-13T22:16:00
David Norman
  • 19,396
  • 12
  • 64
  • 54
  • 2
    In ruby use `iso8601` method, eg `Time.now.iso8601`. – Zubin Mar 14 '14 at 20:33
  • 2
    Out of curiosity, ISO8601 format should look something like `2009-03-18T22:16:00-05:00`, but David's answer (omitting the timezone) works for me too (whereas the ISO8601 format doesn't). Is this because of a local serialization in the XmlConverter (which therefore doesn't need the timezone)? – ajiang Jun 30 '15 at 20:17
  • 1
    In ISO8601, the timezone is optional, so both my original string and your string are valid ISO8601 date time. According to http://www.w3.org/TR/xmlschema-2/#deviantformats, the timezone is also optional in XML schema. I'm not sure why the time zone version doesn't work for you. – David Norman Jul 06 '15 at 21:08
  • 1
    For php Artisans:) there is `toIso8601String` method in `nesbot/carbon` php lib. – userlond Oct 21 '15 at 04:53
  • 1
    in php you can use date('c', $time); – relipse Jun 20 '19 at 14:52
55

Xml readers generally expect dates/times in a very specific format; you can use this yourself using XmlConvert:

string s = XmlConvert.ToString(DateTime.Now);
DateTime when = XmlConvert.ToDateTime(s);

If you are using something else, you'll have to read it as a string and use DateTime.TryParseExact (or similar) to specify the actual format string:

string s = reader.ReadContentAsString();
DateTime when = DateTime.ParseExact(s, "M/d/yy hh:mm tt",
     CultureInfo.InvariantCulture);

If you are using XmlSerializer, you could use a shim property to do the conversion - let me know if this is what you are doing...

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • The data I was feeding in happened to be 10/29/2012 15:25 and that failed. When I changed it to 10/11/2012 15:25, it worked. – Brian Leeming Nov 12 '12 at 21:46
  • @MarcGravell, when i try to deserialize some rss feed using `XmlSerializer`, `PubDate` element causes error. how can i fix it? – burhan Mar 11 '14 at 00:21
  • 2
    @burhan by looking at what the value is coming in as, and handling it appropriately? Alternatively, the core framework includes RSS-targeted classes that may do a better job. – Marc Gravell Mar 11 '14 at 07:46
  • @MarcGravell, it gives the error `System.InvalidOperationException`. can you give the codes to fix this manually? – burhan Mar 11 '14 at 21:22
  • 1
    @burhan not without seeing the xml and your model, no – Marc Gravell Mar 11 '14 at 21:57
  • pls visit [this link](http://diggthedrazen.com/2011/07/05/xml-deserialization-on-windows-phone/) . my codes use the same algorithm. – burhan Mar 11 '14 at 22:05
  • @MarcGravell, did you visit that link? if so, what is the solution? (someone has the same problem in the comments at the end of the page) – burhan Mar 12 '14 at 22:29
  • 2
    @burhan the solution is, as always, to have xml and a model that match. Sometimes that means you can go direct - you might need to have the dates as a string type (rather than DateTime) and then do additional post-processing on it to parse the time. A random link, however, does not provide the exact xml you are struggling with. – Marc Gravell Mar 12 '14 at 22:38