Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF error when converting datetime to json format

Tags:

json

c#

wcf

I have a WCF service that returns JSON.

Since this morning, I started to get the following error:

DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON

Just for the test, I passed today's date to all the DateTime variables that are being returned using JSON, but I stil get the same error.

The code is around 2k rows, so I see no value in posting it here.

Any idea how to solve it??

like image 210
m0fo Avatar asked Oct 11 '12 12:10

m0fo


1 Answers

I suspect you have a DateTime value that is uninitialized - defaults to DateTime.MinValue local time. This can not be converted to UTC if your local timezone is ahead of UTC, because doing so would result in a negative Ticks value.

Either find the uninitialized value and correct it, or move to the USA :)

Another solution might be to use a nullable value (DateTime? in place of DateTime).

This defaults to null rather than DateTime.MinValue, so you should be able to serialize an uninitialized value.

like image 114
Joe Avatar answered Oct 30 '22 14:10

Joe