Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET webservice datetime.now-时区_.net_Datetime_Web Services_Timezone - Fatal编程技术网

.NET webservice datetime.now-时区

.NET webservice datetime.now-时区,.net,datetime,web-services,timezone,.net,Datetime,Web Services,Timezone,我需要从Web服务返回服务器时间。我有以下代码: <WebMethod()> _ Public Function GetDate() As DateTime Return DateTime.Now End Function _ 公共函数GetDate()作为日期时间 返回日期时间,现在 端函数 代码总是根据连接的客户端的时区返回时间(如果我更改时区,Web服务将返回更新的时间,而不是本地服务器时间)。我怎样才能解决这个问题呢?这里的人不急,所以这样标记问题是没有

我需要从Web服务返回服务器时间。我有以下代码:

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

End Function
_
公共函数GetDate()作为日期时间
返回日期时间,现在
端函数

代码总是根据连接的客户端的时区返回时间(如果我更改时区,Web服务将返回更新的时间,而不是本地服务器时间)。我怎样才能解决这个问题呢?

这里的人不急,所以这样标记问题是没有用的


看看哪一个是谷歌的第一个结果…

服务器实际上发送了本地设置时区中的日期时间。客户端可能会对其进行不同的解释(基于其本地设置)。在服务器上使用UTC时间或使用DateTimeOffset提供时区信息更安全。

尝试以下方法:

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

您需要始终返回utc时间。这样你就知道它不是特定于时区的

<WebMethod()> _
Public Function GetDate() As DateTime
        Return DateTime.Now.ToUniversalTime()
End Function
_
公共函数GetDate()作为日期时间
Return DateTime.Now.ToUniversalTime()
端函数

在所有没有UTC偏移量0的时区,这难道不是不正确的吗?这将生成种类==Utc和本地时间的DateTime。这将序列化为本地时间,utc偏移量为0。不太好。