This project has moved and is read-only. For the latest updates, please go here.

Serialize/Deserialize DateTime.MinValue

Nov 21, 2011 at 10:11 AM

When I serialize a DateTime object with the value equals to DateTime.MinValue, the result of the deserialize is not DateTime.MinValue.
DateTime.MinValue to string is "01/01/0001 00:00:00"
After deserialize, my DateTime object is "01/01/0001 01:00:00" (Not equals to DateTime.MinValue")
I suppose the +1 hour is because my system date is set to GMT+1.

After have debug the serialize, I have note that the function ConvertDateTimeToJavaScriptTicks in WriteDateTimeString of JsonConvert.cs didn't return a ticks of "0". It return a ticks set to "-62135596800000"
Maybe the fix could be to test the DateTime value before try to get the ticks converted to javascript.

I have force the value of this tick to "0".
Now a problem appeared in deserialize.
In ConvertJavaScriptTicksToDateTime of JsonConvert.cs, my ticks is 0. Good
But the code sum it with InitialJavaScriptDateTicks (=621355968000000000).
So my result DateTime is set to "01/01/1970 00:00:00".

How can I fix this problem?

Jan 30, 2012 at 6:58 PM
Edited Jan 30, 2012 at 7:43 PM

See this:

Json.Net uses the Epoch time and not the ticks value of the DateTime object, hence why forcing it to 0 returns the result 01/01/1970 00:00:00