DotNetCore/Answer Detail

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

andro
andro
10 Points
5 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?

Total Answered: 1
Total Marked As Answer: 0
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
Answers
Brian
Brian
Participant
508 Points
69 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

Blog

Active User (0)

No Active User!
 Log In to Chat