AJAX and Dates
AJAX and Dates
While AJAX in general, and Data-Services in particular
becomes more and more in use, you may encounter a
problem of sending / receiving dates when you don't
usually share the same Time Zone settings.
Dates between services
When sending dates over soap the date is serialized
with the date and it's time zone
(The GMT +/- # part which comes straight after
the date itself).
When consuming a service through .Net application you won't notice
any difference, That's mainly because it is being translated to your local time.
What's happens "behind the scene" is that the universal time is being sent
with the "Kind" property of "Utc", then it is being translated to your local time
according to your machine's time zone settings.
And what about your browser?
That's not exactly the situation when the service consumer is your browser.
On some browsers, when receiving dates through JSON, the date sometimes
keep it's original settings of date + time zone. displaying the hour with
date.format("HH:mm") can cause the hour not to be displayed according
to your time zone. I've encountered browsers of the same kind and version
that treat dates differently on different computers so you really cannot determine
what each browser will do.
How to determine what is the client treatments for dates?
can be helpful, you can determine the client's time zone by using it
for a date created on client side, you can compare it with the server's
time zone in many ways : Sending the time-zone offset to the server
and compare the server's time zone there, You can send the time zone offset
along with the date from the server to the client and make your comparison
there. It is all depend on where the date object is in, posted date or response.
Just remember - the getTimezoneOffset returned in minutes and with the opposite
sign (meaning - date with Jerusalem standard time will return the result of -120).
What do you want to do with the date?
After you have this information you have to decide what you want to do with
it. If you want to display your dates according to your time zone, determine what
your browser is doing by comparing the getTimezoneOffset() with the actual time zone
on the server. Then, you might want to set you date according to your policy by using
the setMinutes method .When deciding, you might take into account that on the client
side what you most of the time is displaying, and if you didn't use AJAX you would probably
display the dates according to the server's time zone.
In here - the server sends the time zone offset inside the response (TimeZoneOffsetInMinutes):
var tzGap = resopnse.Date.getTimezoneOffset() * (-1) - response.TimeZoneOffsetInMinutes;
if (tzGap != 0)
//Change the date to the server's time zone
response.Date.setMinutes(response.Date.getMinutes() - tzGap);
Note: What I did was actually changing the date, I cannot change the date's time zone offset
Anyway - you have to omit time zone effect when sending the date to the server.
Dealing with dates with web services was always tricky because of different time-zone settings.
It became evenmore tricky when the client is our browser (on AJAX) and we have to use some
it's back to .Net code again and the date being sent is of "Utc" kind
(meaning that you can extract the client's time zone easily).
You can read about it in my "AJAX and DST difference" post.