views:

339

answers:

4

I need to return server time from a webservice. I've got the following code:

<WebMethod()> _
Public Function GetDate() As DateTime
        Return DateTime.Now

End Function

The code always returns time based on timezone of the connected client (if I change the time zone, the webservice returns updated time, instead of local server time). How can I fix the issue?

A: 

It's not urgent for the people here so it's useless to flag the question that way.

Take a look at http://stackoverflow.com/questions/246498 which was the first Google result...

dbemerlin
A: 

The server actually sends the datetime in timezone of its local settings. The client probably interprets it differently (based on its local settings). Its safer to use UTC-times on the server or use DateTimeOffset to provide timezone information.

Jelle
A: 

Try this :

DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
Seb
Wouldn't this be incorrect in all timezone that don't have UTC offset 0? This would generate DateTime with Kind == Utc and local time. This would be serialized as localtime with utc offset 0. Not great.
FkYkko
A: 

you need to always return the utc time. This way you always know its not time zone specific.

<WebMethod()> _
Public Function GetDate() As DateTime
        Return DateTime.Now.ToUniversalTime()
End Function
Hath