By Dave Capper


2012-07-19 04:03:30 8 Comments

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

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

When deserialised using:

var jsonSerializerSettings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind };
var obj = JsonConvert.DeserializeObject(content, jsonSerializerSettings);

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

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

7 comments

@Adel Nasiri 2019-09-24 14:01:15

As a simple way, you can Convert Date to Ticks for serializing and convert it from Ticks to Date for deserializing:

Serializing:

DateTime date = new DateTime();
ticks = date.Ticks

Deserializing"

Datetime = new Datetime(ticks);

@FabioLux 2018-09-25 12:59:02

To use these settings in serializer, type:

var serializerSettings = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.Local
            };
            var serializer = JsonSerializer.Create(serializerSettings);

@Alexander Milchakov 2017-10-21 08:38:17

This works for me, a timezone is preserved

var jss = new JsonSerializerSettings
    {
         DateFormatHandling = DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling = DateTimeZoneHandling.Local, 
         DateParseHandling = DateParseHandling.DateTimeOffset
    };
var responseObj = JsonConvert.DeserializeObject<dynamic>(body, jss);
return responseObj.Select(s => new {
                    id = s["id"].Value<int>(),
                    date = s["date"].Value<DateTimeOffset>().DateTime,
                });

A JSON body is something like this

[
    {
        "id": 211,
        "date": "2017-10-22T12:00:00+03:00",
        "status": 1
    },
    {
        "id": 212,
        "date": "2017-10-28T12:00:00+03:00",
        "status": 1
    }
]

@FindOutIslamNow 2019-12-09 03:09:17

This is correctly working. Thanks

@txavier 2015-08-28 19:14:10

If you're using .NET WebApi you can add the following to the WebApiConfig.cs file to handle this globally in your application.

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.

@Grey Wolf 2014-06-30 03:43:01

Try using this:

microsoftDateFormatSettings = new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
    DateTimeZoneHandling = DateTimeZoneHandling.Local
};
var items = JsonConvert.DeserializeObject<List<lstObject>>(jsonString, microsoftDateFormatSettings);

I don't know if it will work in all cases but for me it did. You can try some other values for DateTimeZoneHandling or search for more options on Google.

@Peter Ritchie 2012-08-01 16:19:10

It seems to be ignoring DateParseHandling.DateTimeOffset and is using DateParseHandling.DateTime. I would log an issue here: https://github.com/JamesNK/Newtonsoft.Json

@Peter Ritchie 2014-03-18 15:17:22

I do not know if this bug has been fixed.

@Jasmeet 2017-02-15 19:13:49

@PeterRitchie any success resolving this issue? I am using 8.0.3 version but still getting the same issue

@Peter Ritchie 2017-02-16 17:20:14

@JasmeetSingh Shows as being fixed on the Newtonsoft site.

@Jasmeet 2017-02-16 22:05:10

@PeterRitchie yeah I saw that. Problem in my case was there were 2 api's wrapped over one. I had to do the setting changes on the wrapped one. Thanks

@chopikadze 2012-12-17 16:01:37

I'm not sure regarding which version did you use, because at some point of time we had the same problem, then update fixed it...

Your code works wrong for me also, but if I create class like

public class A
{
    public DateTimeOffset startDateTime;
}

and call

var obj = JsonConvert.DeserializeObject<A>(content, jsonSerializerSettings);

everything works as expected. Yes, it's bug for sure, yes, I don't know how to get result exactly as YOU want, but probably, it will help for someone else.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] How can I change property names when serializing with Json.net?

1 Answered Questions

Parsing a datetime string that may or may not have an offset

1 Answered Questions

[SOLVED] DateTimeOffset parse and custom time zone

2 Answered Questions

[SOLVED] DateTimeOffset, not clear understanding

2 Answered Questions

2 Answered Questions

[SOLVED] Have C# code run in a different timezone to local time

  • 2013-08-02 10:30:38
  • tom redfern
  • 560 View
  • 1 Score
  • 2 Answer
  • Tags:   c# xml

Sponsored Content