简体   繁体   English

服务器和客户端时区的日期时间差

[英]Datetime difference on server and client side timezone

I have a web application in which my server is hosted in US and my client is from India. 我有一个Web应用程序,其中的服务器托管在美国,我的客户来自印度。 I have a datetime picker ,which I validate on the server whether the client and server datetime is match or not, but the problem is it will cause invalid due to different timezones of client and server. 我有一个日期时间选择器,可以在服务器上验证客户端和服务器的日期时间是否匹配,但是问题是由于客户端和服务器的时区不同,它将导致无效。 How do I solve the date time issue 我该如何解决日期时间问题

Convert time to UTC in your browser and deal with UTC everywhere (except displaying to user) and send date to server in full ISO8601 format so when parsed by server it will be able to convert to its local timezone: 在您的浏览器中将时间转换为UTC并可以在任何地方处理UTC(向用户显示除外),并以完整的ISO8601格式将日期发送到服务器,因此当服务器解析时,它将能够转换为其本地时区:

// JavaScript: myDateTimeValue should be of type Date
var utcDateTimeAsString = myDateTimeValue.toISOString();

Than either set this to hidden field that will be send to server (if using regular postback) or just send as part of your AJAX request. 而不是将此字段设置为将发送到服务器的隐藏字段(如果使用常规回发),或者仅作为AJAX请求的一部分发送。

Server side - regular parsing of such string will produce valid local date that corresponds to the same absolute time: 服务器端-此类字符串的常规解析将产生对应于相同绝对时间的有效本地日期:

// C# parsing of result of JavaScript call: (new Date()).toISOString();
DateTime localTime = DateTime.Parse("2016-02-01T06:38:05.609Z");

Links if you decide to deal with timezones manually: 如果您决定手动处理时区,则链接:

更改服务器的DateTime设置以匹配印度时区而不是美国。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM