Click or drag to resize
Json.NETSerializing Dates in JSON
 

DateTimes in JSON are hard.

The problem comes from the JSON spec itself: there is no literal syntax for dates in JSON. The spec has objects, arrays, strings, integers, and floats, but it defines no standard for what a date looks like.

Dates and Json.NET

The default format used by Json.NET is the ISO 8601 standard: "2012-03-19T07:22Z".

Prior to Json.NET 4.5 dates were written using the Microsoft format: "\/Date(1198908717056)\/". If you want to use this format, or you want to maintain compatibility with Microsoft JSON serializers or older versions of Json.NET, then change the DateFormatHandling setting to MicrosoftDateFormat.

The DateTimeZoneHandling setting can be used to convert a DateTime's DateTimeKind when serializing. For example set DateTimeZoneHandling to Utc to serialize all DateTimes as UTC dates. Note that this setting does not effect DateTimeOffsets.

If your dates don't follow the ISO 8601 standard, then the DateFormatString setting can be used to customize the format of date strings that are read and written using .NET's custom date and time format syntax.

DateTime JsonConverters

With no standard for dates in JSON, the number of possible different formats when interoping with other systems is endless. Fortunately Json.NET has a solution to deal with reading and writing custom dates: JsonConverters. A JsonConverter is used to override how a type is serialized.

DateTime JsonConverters Example
 1public class LogEntry
 2{
 3    public string Details { get; set; }
 4    public DateTime LogDate { get; set; }
 5}
 6
 7public void WriteJsonDates()
 8{
 9    LogEntry entry = new LogEntry
10    {
11        LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
12        Details = "Application started."
13    };
14
15    // default as of Json.NET 4.5
16    string isoJson = JsonConvert.SerializeObject(entry);
17    // {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
18
19    JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
20    {
21        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
22    };
23    string microsoftJson = JsonConvert.SerializeObject(entry, microsoftDateFormatSettings);
24    // {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
25
26    string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
27    // {"Details":"Application started.","LogDate":new Date(1234656000000)}
28}

Simply pass the JsonConverter you wish to use to the Json.NET serializer.

JavaScriptDateTimeConverter

The JavaScriptDateTimeConverter class is one of the two DateTime JsonConverters that come with Json.NET. This converter serializes a DateTime as a JavaScript Date object: new Date(1234656000000)

Technically this is invalid JSON according to the spec, but all browsers and some JSON frameworks, including Json.NET, support it.

IsoDateTimeConverter
Note Note

From Json.NET 4.5 and onwards dates are written using the ISO 8601 format by default, and using this converter is unnecessary.

IsoDateTimeConverter serializes a DateTime to an ISO 8601 formatted string: "2009-02-15T00:00:00Z"

The IsoDateTimeConverter class has a property, DateTimeFormat, to further customize the formatted string.

See Also