I am having trouble deserializing datetimes with Json.Net 6.0.3 (I can reproduce the problem in version 6.0.6). The code runs in .Net 4.5 on Windows 8.1, and the culture is en-GB.
This demonstrates the problem:
using Newtonsoft.Json; using Newtonsoft.Json.Linq; var d1 = new DateTimeOffset(2014, 12, 15, 18, 0, 0, TimeSpan.FromHours(1)); var obj = new { time = d1 }; var json = JsonConvert.SerializeObject(obj, Formatting.Indented); Console.WriteLine(json); var jo = JObject.Parse(json); Console.WriteLine(jo.Value<string>("time") + " // jo.Value<string>(\"time\")"); Console.WriteLine(jo["time"] + " // jo[\"time\"]");
conclusion:
{ "time": "2014-12-15T18:00:00+01:00" } 12/15/2014 17:00:00 // jo.Value<string>("time") 15/12/2014 17:00:00 // jo["time"]
Date time varies depending on how you access the JObject - one of them is MM / DD / YYYY DD / MM / YYYY. Why is this?
I do not need them in a specific format: the problem is that the format is changing. I have a lot of legacy code that parses a datetime string derived from Json.Net. The code will also run on different computers around the world, possibly with different cultures.
Is there a way to force Json.Net to always return datetime in the same format?
source share