如何将 Javascript 创建的 UTC 日期与 C# 中创建的 UTC 日期进行比较?

发布于 2024-11-26 23:22:54 字数 1082 浏览 0 评论 0原文

我正在尝试在一台服务器上用 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# 中,如何从查询字符串中获取该值并将

  1. 其转换为正确的日期,并将
  2. 其与基于 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

  1. convert it into a proper date, and
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-03 23:22:54

JavaScript UTC 方法 返回自 00 以来的毫秒数1970 年 1 月 1 日 :00:00 UTC。要将这些毫秒转换回 C# 中的 DateTime,您只需将它们添加到原始“纪元”中:

string rawMilliseconds = Request.QueryString["expiry"];

if (string.IsNullOrWhiteSpace(rawMilliseconds))
    throw new InvalidOperationException("Expiry is null or empty!");

long milliseconds;
if (!long.TryParse(rawMilliseconds, out milliseconds))
    throw new InvalidOperationException("Unable to parse expiry!");

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

DateTime expiry = epoch.AddMilliseconds(milliseconds);

The JavaScript UTC method returns the number of milliseconds since 00:00:00 UTC on 1 January 1970. To convert those milliseconds back to a DateTime in C# you just need to add them onto that original "epoch":

string rawMilliseconds = Request.QueryString["expiry"];

if (string.IsNullOrWhiteSpace(rawMilliseconds))
    throw new InvalidOperationException("Expiry is null or empty!");

long milliseconds;
if (!long.TryParse(rawMilliseconds, out milliseconds))
    throw new InvalidOperationException("Unable to parse expiry!");

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

DateTime expiry = epoch.AddMilliseconds(milliseconds);
娇纵 2024-12-03 23:22:54

日期时间对象表示时间上的一个瞬间,通常在内部表示为自纪元以来的毫秒数。在 JavaScript 中获取当前时间的该值更容易使用

new Date().getTime()

现在您只需一个数字(或包含数字的字符串),您可以将其传递到 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

new Date().getTime()

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.) :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文