I am not 100% sure what you are looking for here, but I think it is unsafe to assume that JSON.Net will satisfy all your needs without a little help. Like Mr. Newton says :
Dates in JSON are difficult.
Firstly, you need to determine whether you want to support receiving unspecified dates or whether you think that all incoming dates are universal, even if they do not have a trailing Z.
If you assume that all incoming dates are universal, you can just see if they have a final Z and, if not, add it (not exactly production code, but you get the idea):
if (!dateString.EndsWith("Z\"", StringComparison.InvariantCultureIgnoreCase)) { dateString = dateString.Substring(0, dateString.LastIndexOf("\"", StringComparison.InvariantCultureIgnoreCase)) + "Z\""; }
This change in assumption requires that the dates you test for modification be Utc.
If you do not want to assume that the incoming dates are universal, but instead consider them unspecified, you need to change the way the incoming JSON is converted, replacing:
JsonSerializerSettings settings = new JsonSerializerSettings(); settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; settings.DateFormatHandling = DateFormatHandling.IsoDateFormat; DateTime dateSerialized = JsonConvert.DeserializeObject<DateTime>(dateString, settings);
with:
var oConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter(); DateTime dateSerialized = JsonConvert.DeserializeObject<DateTime>(dateString, oConverter);
This will result in an undefined date that exactly matches dateString. This is where your hand helps:
if (dateSerialized.Kind == DateTimeKind.Unspecified) { dateSerialized = dateSerialized.ToUniversalTime(); }
This means that the full, revised first test will look like this and it will pass:
string dateString = "\"2014-06-02T21:00:00.0000000\""; DateTime dateRaw = new DateTime(2014, 6, 2, 21, 0, 0, 0, DateTimeKind.Unspecified); DateTime dateRawAsUtc = new DateTime(2014, 6, 3, 4, 0, 0, 0, DateTimeKind.Utc); dateRawAsUtc.Should().Be(dateRaw.ToUniversalTime()); var oConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter(); DateTime dateSerialized = JsonConvert.DeserializeObject<DateTime>(dateString, oConverter); if (dateSerialized.Kind == DateTimeKind.Unspecified) { dateSerialized = dateSerialized.ToUniversalTime(); } Console.WriteLine("date string: " + dateString); Console.WriteLine("date serialized: " + dateSerialized.ToString("o")); dateSerialized.Kind.Should().Be(DateTimeKind.Utc); dateSerialized.Should().Be(dateRaw.ToUniversalTime()); dateSerialized.Should().Be(dateRawAsUtc);