开发者

.NET webservice datetime.now - time zone

开发者 https://www.devze.com 2022-12-22 07:14 出处:网络
I need to return server time from a webservice. I\'ve got the following code: <WebMethod()> _ Public Function GetDate() As DateTime

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, in开发者_Go百科stead of local server time). How can I fix the issue?


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.


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

Take a look at Creating a DateTime in a specific Time Zone in c# fx 3.5 which was the first Google result...


Try this :

DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号