I have my ASP.NET webservice whicn runs on my local computer in my test environment. My local computers UTC time is +1 (denmark). Now I have these methods available to my javascript client:
[WebMethod(EnableSession=true)]
public DateTime GetDateTime() {
return new DateTime(2013,3,15,10,0,0);
}
[WebMethod(EnableSession=true)]
public void SetDateTime(DateTime DT) {
}
When I call the first method from javascript and alerts the passed DateTime, it correctly shows the 10 AM time: 'Fri Mar 15 2013 10:00:00 GMT+0100 (Romance Standard Time)' Immediately after this alert, I pass the DateTime object back to the webserver. But when I debug this object, the DateTime is now 9 AM. How come? The client and the server runs on the same machine. I´ve played around with DateTime.SpecifyKind, but overall the result is the same; the client subtracts an hour when the time is sent back to the server.
Hope you can help me get a better understanding of this subject.
Thank you in advance.
DateTime
handles timezones terribly. You should either explicitly pass around the time-zone between JS and the server (not really sure how withWebMethod
s though), use UTC throughout and only deal with the timezone when presenting the time to the user, or try if usingDateTimeOffset
helps. (DateTimeOffset
should be properly time-zone-aware, but might not Just Work with a JSON service.) – millimoose Mar 4 at 12:52DateTimeOffset
suggestion was just a longshot - either swapping it in forDateTime
will instantly fix the bug you're having (and then you can just convertDateTimeOffset
to whichever kind ofDateTime
in the rest of your code), or it's not worth investigating further. – millimoose Mar 4 at 13:39