How to get JSON.NET to serialize date and time for ISO 8601?

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.

+12
json asp.net-web-api iso8601
source share
2 answers

The format you get is the ISO 8601 format (see the Wikipedia time and time zones section), it's just that your dates do not seem to be set to UTC, so you get the time zone offset added to the date and not the time zone indicator Z Zulu.

IsoDateTimeConverter has settings that you can use to configure your output. You can automatically set dates in UTC by setting DateTimeStyles to AdjustToUniversal . You can also adjust the output format to omit fractional seconds if you do not want to. By default, the converter is not configured for UTC time and includes as many tens of accuracy values ​​as are available for seconds.

Try the following:

 IsoDateTimeConverter converter = new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AdjustToUniversal, DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK" }; config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(converter); 

If your dates are already set in UTC, but DateTimeKind is not installed on them on Utc , as it should be (for example, it is Unspecified ), then ideally you should fix your code so that this indicator is set correctly before serialization. However, if you cannot (or do not want) to do this, you can get around this by changing the converter settings to always turn on the Z indicator in the date format (instead of using the K qualifier that looks at DateTimeKind for the date) and removes the AdjustToUniversal directive.

 IsoDateTimeConverter converter = new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'" }; 
+21
source share

Adding to @Brian Rogers answer, for ASP Core add to Startup.cs :

 services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddJsonOptions(options => options.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AdjustToUniversal })); 
0
source share

All Articles