I have a Web API application that returns JSON for consumers who may not be using Microsoft technology. When my controller returns an object with DateTime properties as JSON, it serializes the date in this format:
2017-03-15T00:00:00-04:00
This gives the consumer a bit of a headache as they expect it to be in ISO 8601 format. Some studies tell me that JSON.NET now uses ISO 8601 by default (I use 9.0.1). When I run this code ...
Clipboard.Copy(JsonConvert.SerializeObject(DateTime.Now));
... I get this:
2017-03-15T09:10:13.8105498-04:00
Wikipedia shows them as valid ISO 8601 formats when expressing the full date and time:
2017-03-15T11:45:42+00:00 2017-03-15T11:45:42Z 20170315T114542Z
However, the conclusion that I received above does not exactly correspond to any of them. I want the formatter to use 2017-03-15T11:45:42Z .
And probably worthy of another question, adding the below line in my web API configuration seems ignored as it keeps returning JSON on the date originally shown above:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
I assume that as soon as I find out the main problem, the web API problem can also be solved.
oscilatingcretin
source share