Json date parsing unexpected results

Link to example of “error”: https://gistlyn.com/?gist=8849368b21d4fe73750bd9fccb151def&collection=2cc6b5db6afd3ccb0d0149e55fdb3a6a

Not quite sure what is going on, have never noticed this before, but raw dates like “IssueDate”:“02/12/2018” are switching to 12/02/2018 when deserialized. How do I prevent this behavior?

This is culture dependent and thus highly fragile which is why it’s recommended to only send dates in the deterministic standard date format.

Agreed, but it is a vendors data feed so I don’t have the luxury of dictating their data practices.

If that’s the case, I’d personally change the DateTime fields to string and manually parse them in your Service:

DateTime issueDate = DateTime.ParseExact(request.IssueDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);

You could also replace the DateTime parsing with your own:

JsConfig<DateTime>.DeSerializeFn = str => 
    DateTime.ParseExact(str, "MM/dd/yyyy", CultureInfo.InvariantCulture);

But this overrides the default behavior of dates which would only be able to parse dates in that format.

1 Like