2

If you serialize and deserialize a DateTime using embedded .net JavaScriptSerializer, you get two different dates if you are in UTC+something !

Example (suppose you are in UTC+2 like I am now)

JavaScriptSerializer myJson = new JavaScriptSerializer();

DateTime myDate = DateTime.Now; //suppose 2016-03-29 16:12:00
strSerialized = myJson.Serialize(myDate);

//DO WHAT YOU NEED WITH IT...

DateTime myDateDes = myJson.Deserialize<DateTime>(strSerialized);
Label1.Text=myDateDes.ToString();//it gives you 2016-03-29 14:12:00 ! WRONG! IT's in UTC+0 ! Has 2 HOURS less !!!

So, when you get the deserialized date, it'll give you the UTC+0 value by default...!!

This is different from JavaScriptSerializer UTC DateTime issues because that article describes the difference in deserialization of different datetime data types, and provides a solution (.UtcDateTime) that doesn't fix the problem. In fact, trying to deserialize with .utcDateTime a serialized DateTime always gives you the wrong UTC+0 date...

Community
  • 1
  • 1
BitQuestions
  • 651
  • 7
  • 14
  • 1
    I guess you want to serialize/deserialize a DateTimeOffset object instead. – Gábor Bakos Mar 29 '16 at 14:31
  • In my case I needed the DateTime ... the DateTimeOffset gives me the utc offset like 29/03/2016 17:00:23 +02:00 but it always convert to the utc+0 after deserialization (29/03/2016 15:00:23 +00:00). This is useful to understand what happens behind, but In my case I needed simply to ... deserialize exactly what I serialized, not a changed utc version of it... – BitQuestions Mar 29 '16 at 15:22
  • 1
    I believe this is in fact a known bug in the .net serializer. – Jeff Mar 29 '16 at 15:43
  • 2
    There's a bug in your expectations, not necessarily in the implementation. What is the JSON string `"2016-03-29 16:12:00"` (or whatever it serializes to, which you didn't show) supposed to mean, and why? There is no timezone information in that string, so the serializer has to make _some_ assumption. That this assumption is different from the one you made, does not make it a bug. – CodeCaster Mar 29 '16 at 15:46
  • See also [JavaScriptSerializer is subtracting one day from date](http://stackoverflow.com/questions/15003335/), [C# JavaScriptSerializer and DateTime.MinValue crossing timezones](http://stackoverflow.com/questions/16917640/), [Problem deserializating JSON Date in C# - adding 2 hours](http://stackoverflow.com/questions/4304486/) and especially [MSDN: Understanding Literal Notation in JavaScript](https://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_topic2), where all this behavior is documented. – CodeCaster Mar 29 '16 at 15:51
  • CodeCaster: I think more on a bug, because this code was running fine for months until the server automatically moved 1 hour forward because of daylight saving. So when we were UTC+1 the ser/deser was working good, and when we moved to UTC+2 the deser was not matching anymore... – BitQuestions Mar 30 '16 at 07:49

1 Answers1

3

There are two different solutions: either use ToLocalTime() when you deserialize OR use the Newtonsoft.Json.

So the same code, "fixed", in the first case should be:

JavaScriptSerializer myJson = new JavaScriptSerializer();

DateTime myDate = DateTime.Now; //suppose 2016-03-29 16:12:00
strSerialized = myJson.Serialize(myDate);

//DO WHAT YOU NEED WITH IT...

DateTime myDateDes = myJson.Deserialize<DateTime>(strSerialized).ToLocalTime();

Label1.Text=myDateDes.ToString();//it gives you 2016-03-29 16:12:00 !!! CORRECT !

Otherwise, using Newtonsoft.Json (you first need to install it from nuGet, then add a "using Newtonsoft.Json" at the top), and use it like this:

DateTime myDate = DateTime.Now; //suppose 2016-03-29 16:12:00
strSerialized = JsonConvert.SerializeObject(myDate);

//DO WHAT YOU NEED WITH IT...

DateTime myDateDes = JsonConvert.DeserializeObject<DateTime>(strSerialized);
Label1.Text=myDateDes.ToString();//NO need to convert to LocalTime... it already gives you 2016-03-29 16:12:00 !!! CORRECT !

I hope this will be useful for someone else... I googled a lot and found nothing about this problem that only happens with Microsoft serializer...

BitQuestions
  • 651
  • 7
  • 14