.net JSON serializer returns local client time to browser? .net JSON serializer returns local client time to browser? json json

.net JSON serializer returns local client time to browser?


To avoid weird bugs and having to deal with these kinds of issues, you should always deal in UTC and convert to local time at the last possible moment.

How are you examining the DateTime once it arrives in the browser? Are you sure the raw serialized format is not including the offset as part of the DateTime object? In which case, it could reconstitute at the other end in local time


To answer the OPs question, the timezone information is implicit in the conversion to the JSON /Date()/ format, because it is relative to UTC. For example, on my server here in NY, if I return a DateTime.Parse("1/1/1970"), it returns /Date(18000000)/, or, 5 hours (we're in DST now), which is the number of seconds since 1/1/1970 UTC, since the conversion says, "hey, it's 1/1/1970 00:00:00 here in NY, so it must be 1/1/70 05:00:00 back over in Greenwich."

Now, if a client in California received this date notation, and simply instantiates a JavaScript date from the milliseconds (e.g. new Date(18000000)), the browser will say, "hey, here is a date object, which I know is relative to UTC, and I know I am 8 hours from Greenwich, so it must be 12/31/1969 21:00:00."

So this is a pretty clever way to deal with time, so that it is "correct" in all time zones, and such that all localization is handled by the user's browser. Unfortunately, we are often dealing with just a raw date that we don't want to be time zone relative (say, a birthday). If we need to keep the date the same, there are two ways that I know of.

The first, as you have done above, is to adjust the time (although I think you need to do it at the browser, too, if you want it to work in any time zone).

The other way would be to return it as a string, formatted already. This is the method I normally employ, but I am normally working with US clients (so I can return MM/DD/YYYY, for example, and they don't get mad at me for being American).


I've just ran into the same issue. It seems that the Json Serializer returns dates in the default timezone that the system is in (essentially disregarding whatever timezone the DateTime is in).

e.g.:On my dev machine, it returns the dates in the timezone my machine is in (pacific); on our production machine the JSON dates are in UTC - which is the timezone the server is set to.

To solve this, in our case we had to manually add hoursoffset, minutesoffset, and daylight savings time offset in the client via javascript.