Json.NET Deserialize Object change the timezone to local time?

28 Points
9 Posts

I'm using json.net ' JsonConvert' to deserialize a DateTimeOffset, but it is ignoring the specified timezone and converting the datetime to the local offset. For example, as

var content = @"{""startDateTime"":""2018-07-19T14:30:00+09:30""}";

When deserialised using:

var obj = JsonConvert.Deserialize<Object>(content);

The json obj returning a property containing a DateTimeOffset but the value will be 2018-07-19T15:30:00+10:30 i.e. converted to the local timezone instead of preserving the original timezone.

Is there any way to get the value to be parsed as expected so that the resulting DateTimeOffset property will match the supplied one?

Views: 21392
Total Answered: 2
Total Marked As Answer: 1
Posted On: 08-Apr-2018 00:35

Share:   fb twitter linkedin
It seems to be ignoring DateParseHandling.DateTimeOffset and is using DateParseHandling.DateTime
 - Rahul Maurya  28-Apr-2018 05:01
694 Points
92 Posts

Try to use:

var content = @"{""startDateTime"":""2018-07-19T14:30:00+09:30""}";

var jss = new JsonSerializerSettings
         DateFormatHandling = DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling = DateTimeZoneHandling.Local,
         DateParseHandling = DateParseHandling.DateTimeOffset

var obj = JsonConvert.Deserialize<Object>(content, jss);
Posted On: 28-Apr-2018 05:16
4 Points
2 Posts

If you're using WebApi you can configure it globally by adding in the WebApiConfig.cs :

= 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.

Posted On: 22-Dec-2018 21:59
 Log In to Chat