如何将 Javascript 创建的 UTC 日期与 C# 中创建的 UTC 日期进行比较?
我正在尝试在一台服务器上用 JavaScript 创建一个 UTC 日期,然后通过 URL 查询字符串将其传递到另一台服务器,其中 C# 可以采用该查询字符串,将其识别为日期并将其与新的 C# UTC 日期进行比较 - 这证明了不过,这比我更棘手(除非我只是度过那些日子之一)。我在 stackoverflow 上没有看到任何其他问题(在输入问题时显示的“类似标题”或“类似问题”列表中)。
为了在 JavaScript 中创建数据,我使用以下命令,基于这篇 w3schools 文章:
var currentDate = new Date();
var day = currentDate.getUTCDate();
var month = currentDate.getUTCMonth();
var year = currentDate.getUTCFullYear();
var hours = currentDate.getUTCHours();
var minutes = currentDate.getUTCMinutes();
var seconds = currentDate.getUTCSeconds();
var milliseconds = currentDate.getUTCMilliseconds();
var expiry = Date.UTC(month,day,year,hours,minutes,seconds,milliseconds);
结果看起来像这样 1311871476074
那么,在 C# 中,如何从查询字符串中获取该值并将
- 其转换为正确的日期,并将
- 其与基于 C# 的 UTC 进行比较日期时间变量?
任何提示、对我的逻辑/代码的更正或文章链接将不胜感激。
凯文
更新
下面的两个答案都帮助我解决了我的问题:Luke 帮助解决了 C# 方面的问题,Ray 帮助解决了 JavaScript - 不幸的是,我无法将它们都标记为答案,但我希望我可以!
I'm trying to create a UTC date in JavaScript on one server, and pass it via URL querystring to another server, where C# can take that querystring, recognize it as a date and compare it to a new C# UTC date - and it's proving trickier that I though (unless I'm just having one of those days). I don't see any other questions on stackoverflow asking this (in the 'similar titles' or 'similar questions' lists that show while typing a question).
To create the data in JavaScript I'm using the following, based on this w3schools article:
var currentDate = new Date();
var day = currentDate.getUTCDate();
var month = currentDate.getUTCMonth();
var year = currentDate.getUTCFullYear();
var hours = currentDate.getUTCHours();
var minutes = currentDate.getUTCMinutes();
var seconds = currentDate.getUTCSeconds();
var milliseconds = currentDate.getUTCMilliseconds();
var expiry = Date.UTC(month,day,year,hours,minutes,seconds,milliseconds);
the results looks like this 1311871476074
So, in C# how do I take this value from the querystring and
- convert it into a proper date, and
- compare it to a C# based UTC DateTime variable?
Any tips, corrections in my logic/code or links to articles would be greatly appreciated.
Kevin
UPDATE
Both the answers below helped me resolve my issue: Luke helped with the C# side of things, and Ray helped with the JavaScript - unfortunately I can't mark them both as answers, but I wish I could!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript
UTC
方法 返回自 00 以来的毫秒数1970 年 1 月 1 日 :00:00 UTC。要将这些毫秒转换回 C# 中的DateTime
,您只需将它们添加到原始“纪元”中:The JavaScript
UTC
method returns the number of milliseconds since 00:00:00 UTC on 1 January 1970. To convert those milliseconds back to aDateTime
in C# you just need to add them onto that original "epoch":日期时间对象表示时间上的一个瞬间,通常在内部表示为自纪元以来的毫秒数。在 JavaScript 中获取当前时间的该值更容易使用
现在您只需一个数字(或包含数字的字符串),您可以将其传递到 C# 应用程序并从中构造一个 DateTime 对象。
为此,我可以参考您C# 将 UTC int 转换为 DateTime 对象 (乔恩·斯基特有一个答案。):)
A datetime object represents an instant in time and is usually represented internally as the number of milliseconds since the epoch. In JavaScript to get this value for the current time it is easier to use
Now you just have a number (or a string containing a number) which you can pass to your C# application and construct a DateTime object from.
For that I can refer you to C# convert UTC int to DateTime object (Jon Skeet has an answer for it.) :)