JavaScriptSerializer is subtracting one day from date

前端 未结 3 1822
天涯浪人
天涯浪人 2020-12-10 01:18

I am using JavaScriptSerializer for serializing DateTime, but when I deserialize it show one day less from the date it get serialize:

Here is test:

3条回答
  •  星月不相逢
    2020-12-10 02:14

    On deserializing JavaScriptSerializer giving me output in UTC (Universal Time) which due to change in hours change the date. As Brad Christie suggested to change DateTime to UTC it can solve the problems.

    But actually there is no need to change the:

    DateTime startDate = new DateTime(2012, 1, 20).ToUniversalTime();
    

    as it is already taking it as Universal Time. So I just convert the output of deserialize to LocalTime:

     DateTime afterDeserialize= serializer.Deserialize(serializeDate);
     afterDeserialize.ToLocalTime();
    

    it solved the issue.

提交回复
热议问题