I am using Json.net 7.0.1 in C# to consume a rest API. The trouble is with the date format the API uses in its JSON response. It looks like this:
/Date(1445301615000-0700)/
That is meant to represent a UTC time of 2015-10-19 17:40:15
If you plug 1445301615000 into an epoch time converter, you see that it is 2015-10-20 00:40:15 . So it is 7 hours ahead of UTC. Then they include the -0700 presumably to offset that back to UTC. So, in an effort to give me a UTC time, they are sending me UTC+7-0700. Why they do it this way I have no idea, but I can't change that.
My question is, how best to make Json.NET parse that date string and come up with a DateTime of 2015-10-19 17:40:15 UTC. I can write a custom JsonConverter to hijack the value and manipulate it manually, but I was wondering if there is a more native method.
I have tried changing the JsonSerializerSettings DateTimeZoneHandling property to all its different values. Setting it to Utc just ignores the time zone offset, yielding 2015-10-20 00:40:15. Setting it to Local, Unspecified, or RoundtripKind all yield 2015-10-19 20:40:15, which I believe is because my local timezone is UTC-4, so it is trying to apply that adjustment to the main date value of 2015-10-20 00:40.
I also considered using the DateFormatString property to represent the expected date string format. But I could not find the right format string characters to represent this epochtime-offset format.
Here's a simplified example:
Person person;
string json = @"{ 'Name': 'John',
'LastSeen':'/Date(1445301615000-0700)/' }"; // 1445301615000 = 2015-10-20 00:40:15
person = JsonConvert.DeserializeObject<Person>(json);
Console.WriteLine(person.LastSeen); // 10/19/2015 8:40:15 PM Kind = Local
person = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat });
Console.WriteLine(person.LastSeen); // 10/19/2015 8:40:15 PM Kind = Local
person = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Utc });
Console.WriteLine(person.LastSeen); // 10/20/2015 00:40:15 PM Kind = Utc
// In all three, the -0700 portion is being ignored. I'd like person.LastSeen to be 10/19/2015 17:40:15.
Again, I could just know that the API is going to give me UTC+7 and do the adjustment myself to get real UTC. But I wondered if Json.NET has a native way to handle this type of date string.