0

Newtonsoft.Json.JsonConvert.DeserializeObject throws invalid Globalization date format exception for following date:

2016-07-26T24:33:37.177Z

Code

DataTable leadDataTable = (DataTable)JsonConvert.DeserializeObject(leadAObj["result"].ToString(),
    typeof(DataTable));

I tried by explicitly passing date format

Code

JsonConvert.DeserializeObject(leadAObj["result"].ToString(), typeof(DataTable),
     new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ" })
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
Ayyappa P
  • 27
  • 7
  • Have you tried settings the `JsonSerializerSettings.DateFormatString` property to the required datetime format? – Jamie Rees Aug 19 '16 at 11:43
  • I passed IsodateTimeConverter object as third parameter to Deserialize Object method, *New Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ" }* – Ayyappa P Aug 19 '16 at 11:45
  • 1
    That *is* an invalid ISO8601 time. There is no `24:33`, it should be `00:33`. You should fix the code that generates the JSON string – Panagiotis Kanavos Aug 19 '16 at 11:56
  • To put it lightly, the 25th hour may be valid for movies but not for ISO dates – Panagiotis Kanavos Aug 19 '16 at 11:59
  • I'm getting this data from REST api call, with out Deserialize how could I update that data, please suggest some way, I don't have acces to update that api code – Ayyappa P Aug 19 '16 at 12:00
  • That sounds like a bug in the API, and the API should be fixed. If you have to fix it client side then you'd need to search for the T24 and change it to T00 (and then decide whether the date should be 26th or the 27th) All very hacky though, better to fix the API? – Slicc Aug 19 '16 at 12:03
  • `DateTime.Parse()` itself throws an exception when passed a date string like that. You may have to write your subclass of `IsoDateTimeConverter` and parse the date string manually. See [Parsing JSON dates that end in T24:00:00Z](https://stackoverflow.com/questions/32640773/parsing-json-dates-that-end-in-t240000z) for a similar question. – dbc Aug 19 '16 at 15:50

1 Answers1

4

The time is 33 minutes past midnight is that what you are expecting?

Shouldn't it be 00:33 not 24:33

Slicc
  • 3,217
  • 7
  • 35
  • 70