Problem deserializating JSON Date in C# - adding 2 hours
Asked Answered
S

4

7

We are having such a nasty problem when deserializating a JSON date to a C# DateTime.

The code is:

JavaScriptSerializer serializer = new JavaScriptSerializer();
jsonTrechos = jsonTrechos.Replace("/Date(", "\\/Date(").Replace(")/", ")\\/");
Trecho[] model = serializer.Deserialize<Trecho[]>(jsonTrechos);

The jsonTrechos is a string of json2.js's JSON.stringify();.

Problem is: the deserialization works, bur all dates of the Trechos objects are added with 2 hours.

My timezone is Brazil (UTC -3) and we're under daylight savings (so we're currently on UTC -2), if it has anything to do. I guess that perhaps localization and timezones may be playing a part on this and if they really are, I have no idea on how to fix it.

Stratagem answered 29/11, 2010 at 14:5 Comment(2)
Btw, those Replaces are there so the serializer can recognize the "\/Date(number)\/" format.Stratagem
I recommend using UTC dates internally unless there are compelling reasons not to. And in the cases where you need non-UTC dates a simple local date doesn't work well either, but you typically need something more complex taking care of timezones.Grin
O
13

This is documented in the MSDN:

Date object, represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

Try calling DateTime.ToLocalTime() and see if you get the correct date for you.

Occidentalize answered 29/11, 2010 at 14:20 Comment(2)
@scardazzi #1264232 has some info about parsing too which I've been stung with beforeConk
This got me on the right track. I am using Json.NET to deserialize some JSON and had to set my DateTimeZoneHandling property to DateTimeZoneHandling.Local.Hebrides
W
7

I would strongly recommend working with the Json.NET library. Quite frankly, the JSON serializers (and there are multiple ones) in the .NET framework are all quirky in some way, especially when it comes to serializing dates.

Json.NET is the only library that I've seen that handles them (and JSON in general) consistently and without problem for other consumers.

Wakerife answered 29/11, 2010 at 14:13 Comment(2)
Makes sense, I've played with it a little and I liked it. I'll check it out, thanks!Stratagem
This is way better than the javascriptserializer class in terms of date handling and performance, thanks!!Darrelldarrelle
C
2

The dates specified for JSON are UTC, and as you've mentioned you're using daylight savings so +2 hours makes sense. Ideally you should be working with UTC date times anyway, as it removes the headaches of daylight savings (or in this case, it's added to it) and allows for global hosting.

Conk answered 29/11, 2010 at 14:23 Comment(1)
You're right, it'd be the best to do this, however it'll take such a long time to adapt this system to this. But thanks for the advice!Stratagem
P
1

"Javascript dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds" (Excerpt from W3schools). So you want to convert it to your local timezone.

TimeZoneInfo.ConvertTimeFromUtc(yourDateToConvert, TimeZoneInfo.Local)
Paragraphia answered 7/3, 2011 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.