DateTimes in JSON are hard.

The problem comes from the JSON spec http://www.ietf.org/rfc/rfc4627.txt _blank 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 http://en.wikipedia.org/wiki/ISO_8601 _blank : "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 T:Newtonsoft.Json.DateFormatHandling setting to MicrosoftDateFormat.

Json.NET also has the T:Newtonsoft.Json.DateTimeZoneHandling setting. This can be used to convert DateTime's kind when serializing, e.g. set DateTimeZoneHandling to Utc to serialize all DateTimes as UTC dates.

# 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

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 http://msdn.microsoft.com/en-us/library/cd9w2te4.aspx _blank : 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

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

IsoDateTimeConverter seralizes a DateTime to an ISO 8601 http://en.wikipedia.org/wiki/ISO_8601 _blank formatted string: "2009-02-15T00:00:00Z"

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

T:Newtonsoft.Json.DateFormatHandling
T:Newtonsoft.Json.DateTimeZoneHandling
T:Newtonsoft.Json.Converters.JavaScriptDateTimeConverter
T:Newtonsoft.Json.Converters.IsoDateTimeConverter