Reputation: 243
I have a problem with deserialization of datetime. In JSON it comes in this format 2016-10-04T15:20:00 but after deserialialization it changes to the AM/PM time format and I need to preserve the 24 hour format. Is there any way how specify the format?
Upvotes: 0
Views: 529
Reputation: 129827
When you deserialize to a date, the format is not stored inside the date object. Instead, the formatting happens on output. The default format for your locale is probably using 12-hour time. If you want a different format, you can pass a format string to the ToString
method:
string json = @"{ ""date"": ""2016-10-04T15:20:00"" }";
Foo foo = JsonConvert.DeserializeObject<Foo>(json);
Console.WriteLine(foo.Date.ToString("yyyy-MM-dd HH:mm:ss"));
Fiddle: https://dotnetfiddle.net/ibLCbG
Upvotes: 1