I have a ASP.Net Core service that uses Newtonsoft.Json library for working with JSON data. The sample JSON input has a string that holds a date value in ISO8601 format. However I am observing different date time value being returned for an equivalent date. Here is the sample code -
var jsonString = @"{
""data"": {
""name"": ""John Doe"",
""dateOfBirth"": ""1990-05-25T15:54:49.119+00:00""
}
}";
var jsonObj = JObject.Parse(jsonString);
var person = jsonObj.ToObject<Person>();
DateTime dateOfBirth = DateTime.Parse(person.Data.DateOfBirth);
if (dateOfBirth.Kind != DateTimeKind.Utc)
{
dateOfBirth = dateOfBirth.ToUniversalTime();
}
Console.WriteLine("Date of birth is " + dateOfBirth.ToString("o"));
The Person class is like this -
class Person
{
public PersonalData Data;
}
class PersonalData
{
public string Name { get; set; }
public string DateOfBirth { get; set; }
}
If I provide ""dateOfBirth"": ""1990-05-25T15:54:49.119+00:00
"", the output is -
Date of birth is 1990-05-25T15:54:49.0000000Z
If I provide ""dateOfBirth"": ""1990-05-25T15:54:49.119Z""
, the output is -
Date of birth is 1990-05-25T10:24:49.0000000Z
As it can be seen, the output is different where as it should have been same. The callers can set any date time string in ISO8601 format.
Is there any way where this can be handled consistently?