Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve timezone when deserializing DateTime using JSON.NET? [duplicate]

I'm parsing some JSON in C# using JSON.NET. One of the fields in the JSON is a date/time, like this:

{
    "theTime":"2014-11-20T07:15:11-0500",
    // ... a lot more fields ...
}

Note that the time part is 07:15:11 (TZ of GMT-5 hrs)

I parse the JSON from a stream like this:

 using (var streamReader = new StreamReader(rcvdStream))
 {
     JsonTextReader reader = new JsonTextReader(streamReader);
     JsonSerializer serializer = new JsonSerializer();
     JObject data = serializer.Deserialize<JObject>(reader);
     //...
  }

Then access the time:

DateTime theTime = (DateTime)data["theTime"];

However, this gives me this DateTime object:

{20/11/2014 12:15:11}
Date: {20/11/2014 00:00:00}
Day: 20
DayOfWeek: Thursday
DayOfYear: 324
Hour: 12
Kind: Local
Millisecond: 0
Minute: 15
Month: 11
Second: 11
Ticks: 635520825110000000
TimeOfDay: {12:15:11}
Year: 2014

I need to know the original local time and tz offset, but I seem to have lost that information in the deserialization process, and it is giving me the time in what I presume is my local time (I'm in the UK, so currently at GMT+0).

Is there a way for me to preserve the timezone information when deserializing?

EDIT: added more detail about how I'm deserializing.

like image 548
Dave W Avatar asked Nov 20 '14 15:11

Dave W


People also ask

When deserializing dates in JSON what timezone should be used?

When deserializing dates using System.Text.Json, the dates should be deserialized consistently regardless of the timezone of the deserializer's computer. When a TimeZone is not present in the JSON, it is assumed to the the local time zone.

How do I serialize a date in JSON?

Serializing Dates in JSON. DateTimes in JSON are hard. The problem comes from the JSON spec itself: there is no literal syntax for dates in JSON. The spec has objects, arrays, strings, integers, and floats, but it defines no standard for what a date looks like.

Is it possible to convert JSON to date?

Technically this is invalid JSON according to the spec, but all browsers and some JSON frameworks, including Json.NET, support it. Note. From Json.NET 4.5 and onwards dates are written using the ISO 8601 format by default, and using this converter is unnecessary.

What is datetimezonehandling in jsonformatter?

config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local; This will specifically tell the JsonFormatter to include and understand the local time zone information when serializing and deserializing a date.


1 Answers

I would use DateTimeOffset for this instead. DateTime doesn't have any useful time zone information associated with it.

You can deserialize to DateTimeOffset instead by changing serializer.DateParseHandling:

JsonSerializer serializer = new JsonSerializer();
serializer.DateParseHandling = DateParseHandling.DateTimeOffset;

JObject data = serializer.Deserialize<JObject>(reader);

var offset = (DateTimeOffset)data["theTime"];

Console.WriteLine(offset.Offset);    // -5:00:00
Console.WriteLine(offset.DateTime);  // 11/20/2014 7:15:11 AM

Example: https://dotnetfiddle.net/I9UAuC

like image 156
Andrew Whitaker Avatar answered Oct 07 '22 16:10

Andrew Whitaker