0

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.