Skip to content

Latest commit

 

History

History
43 lines (25 loc) · 1.74 KB

File metadata and controls

43 lines (25 loc) · 1.74 KB

Dates in JSON

In the JSON spec 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

DateOnly and TimeOnly

DateOnly and TimeOnly are supported out of the box on net6.0 and above. No converter is required.

Both are written as JSON strings using a fixed invariant-culture format:

Type Format Example
DateOnly yyyy'-'MM'-'dd "2000-12-29"
TimeOnly HH':'mm':'ss.FFFFFFF "13:45:30.1234567"
var json = JsonConvert.SerializeObject(new DateOnly(2000, 12, 29));
// "2000-12-29"

var date = JsonConvert.DeserializeObject<DateOnly>("\"2000-12-29\"");

Nullable (DateOnly? / TimeOnly?) and collection members behave the same way, with null written as null.

DateOnly is parsed using that same format. TimeOnly is parsed with TimeOnly.Parse and the invariant culture, so other time formats are also read, for example "13:45".

Note that the written formats are fixed. DateFormatHandling, DateFormatString and DateTimeZoneHandling apply to DateTime and DateTimeOffset only, and have no effect on DateOnly or TimeOnly. To use a different format, add a custom JsonConverter.

On net462, net472 and net48 the types do not exist, so this support is not compiled in.

Related Topics

  • Argon.DateFormatHandling
  • Argon.DateTimeZoneHandling
  • Argon.JavaScriptDateTimeConverter
  • Argon.IsoDateTimeConverter