Is it possible to instruct JSON.Net to serialize DateTime as UTC even if no time zone is specified? entity-framework javascript


My database stores dates as UTC. But they return as type undefined when I get them using the entity framework.

They are not in UTC format when they are serialized by JSON.Net. Is it possible to instruct JSON.Net to serialize DateTimes as UTC even if that is not their type?

4/24/2012 5:34:51 PM

Accepted Answer

Set DateTimeZoneHandling on JsonSerializerSettings to Utc That will serialize all dates before converting them to UTC.

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);

Zzz-20-Zzz Documentation

6/1/2016 12:26:00 PM

Popular Answer

I utilized the above answer, which is completely functional, to build an attribute that converts API responses from PST to UTC.

I first had to make aJsonConverter

public class UTCDateTimeConverter : Newtonsoft.Json.JsonConverter {
    private TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
    public override bool CanConvert(Type objectType) {
        return objectType == typeof(DateTime);

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        if (reader.Value == null) return null;
        var pacificTime = DateTime.Parse(reader.Value.ToString());
        return TimeZoneInfo.ConvertTimeToUtc(pacificTime, pacificZone);

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
        writer.WriteValue(TimeZoneInfo.ConvertTimeFromUtc((DateTime) value, pacificZone));

When it came time to transform the properties, I had to use that.

public class Order{
    public DateTime OrderDate {get;set;}

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow