简体   繁体   English

从JavaScript客户端发送到.NET的字符串格式的UTC日期已转换为DateTime

[英]UTC date in string format send from JavaScript client to .NET is getting converted into DateTime

I am working with a client application written in JavaScript which sends data to ASP.NET web api end point (server). 我正在使用用JavaScript编写的客户端应用程序,该应用程序将数据发送到ASP.NET Web API端点(服务器)。

I am asking user to select a date & time. 我要用户选择日期和时间。 I convert the that date into UTC datetime as string using . 我使用将该日期转换为UTC日期时间作为string toUTCString().

I send this information to .NET application, which expects the value to be of DateTime type. 我将此信息发送到.NET应用程序,该应用程序期望该值是DateTime类型。

The value I am sending = Thu, 31 Dec 2015 03:30:00 GMT 我发送的值= Thu, 31 Dec 2015 03:30:00 GMT

value I receive on .net side = {12/30/2015 10:30:00 PM} 我在.net端收到的价值= {12/30/2015 10:30:00 PM}

I understand the value is getting converted. 我知道价值正在转化。 My question is: How can I convert the date to UTC on the server side. 我的问题是:如何在服务器端将日期转换为UTC。 My assumption is there is no implicit timezone offset attached to a DateTime object. 我的假设是没有附加到DateTime对象的隐式timezone offset If I run ToUniversalTime() on it when I'm running the code in Greenwich, then I get the same time. 如果我在格林威治代码中运行ToUniversalTime()时,我得到的时间是相同的。 If I do it while I live in Vancouver, then I get an offset DateTime object of -8 hours. 如果我在温哥华居住时这样做,那么我将得到-8小时的偏移DateTime对象。

So I a client coming from different location and I convert that time into UTC by using .ToUniversalTime() , the conversion will be wrong. 因此,我来​​自不同位置的客户端,然后使用.ToUniversalTime()将时间转换为UTC,转换将是错误的。 How do I overcome this problem? 我该如何克服这个问题?

If a user selects date and time I assume it's not current and is relative to the user. 如果用户选择日期和时间,则我认为它不是当前的并且是相对于用户的。 In this case yes, you would want to convert to UTC if you want to further analyze data or present it to another user in another timezone. 在这种情况下,如果您想进一步分析数据或将其提供给另一个时区的其他用户,则需要转换为UTC。 If you don't convert it to UTC, you would receive relative date/time and would need another data that indicates timezone. 如果不将其转换为UTC,则会收到相对的日期/时间,并且需要另一个指示时区的数据。 But even in that case, there is no point other than security measures to compute on the server something that could be easily computed on the client. 但是即使在那种情况下,除了安全措施之外,没有其他事情可以在服务器上计算可以在客户端上轻松计算的东西。

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

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