Newtonsoft.json: yyyyMMddTHHmmssZ ISO8601 is not supported by default by IsoDateTimeConverter

Created on 11 Jul 2018  路  2Comments  路  Source: JamesNK/Newtonsoft.Json

Source/destination types

   public class EmployementHistory
    {
        public string EmployeeName { get; set; }
        public DateTime PositionChangeDate { get; set; }
    }

Source/destination JSON

{ "employeename":"Tom", "PositionChangeDate":"20180710T135034Z" }

Expected behavior

As yyyyMMddTHHmmssZ is an ISO8601 format, it should be supported by default, according to IsoDateTimeConverter documentation

Actual behavior

{System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at Newtonsoft.Json.Converters.IsoDateTimeConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)

Steps to reproduce

  var json = "{ \"employeename\":\"Tom\", \"PositionChangeDate\":\"20180710T135034Z\" }";
  var serializeSettings=new JsonSerializerSettings();
  serializeSettings.Converters.Add(new IsoDateTimeConverter());
  var result= JsonConvert.DeserializeObject<EmployementHistory>(json,serializeSettings);

Most helpful comment

yyyyMMddTHHmmssZ is an ISO8601 format

No it isn't

In your opinion, why is yyyyMMddTHHmmssZ not ISO8601 format?

Per your reference, https://en.wikipedia.org/wiki/ISO_8601, it is (see "Date and time expressed according to ISO 8601" box.

If your statement is due to the missing separators, then per http://www.cl.cam.ac.uk/~mgk25/iso-time.html:

The hyphens can be omitted
...
separating colons can also be omitted

All 2 comments

yyyyMMddTHHmmssZ is an ISO8601 format

No it isn't

https://en.wikipedia.org/wiki/ISO_8601

In representations for interchange, dates and times are arranged so the largest temporal term (the year) is placed to the left and each successively smaller term is placed to the right of the previous term. Representations must be written in a combination of Arabic numerals and certain characters (such as "-", ":", "T", "W", and "Z") that are given specific meanings within the standard

yyyyMMddTHHmmssZ is an ISO8601 format

No it isn't

In your opinion, why is yyyyMMddTHHmmssZ not ISO8601 format?

Per your reference, https://en.wikipedia.org/wiki/ISO_8601, it is (see "Date and time expressed according to ISO 8601" box.

If your statement is due to the missing separators, then per http://www.cl.cam.ac.uk/~mgk25/iso-time.html:

The hyphens can be omitted
...
separating colons can also be omitted

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jskeet picture jskeet  路  20Comments

Bassman2 picture Bassman2  路  20Comments

danebou picture danebou  路  18Comments

davidfowl picture davidfowl  路  49Comments

Richard-Payne picture Richard-Payne  路  13Comments