Dates in my database are stored as Utc. But when I retreieve them w/ the entity framework they come out as type unspecified.
When JSON.Net serializes them they are not in Utc format. Is there a way to tell JSON.Net to serialize DateTimes as Utc even if their type is not specified as Utc?
Set DateTimeZoneHandling on JsonSerializerSettings to Utc. That will convert all dates to UTC before serializing them.
public void SerializeObjectDateTimeZoneHandling() { string json = JsonConvert.SerializeObject( new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified), new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Utc }); Assert.AreEqual(@"""2000-01-01T01:01:01Z""", json); } Documentation: DateTimeZoneHandling setting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With