JS 日期仅包含 UTC 时区

发布于 2024-12-08 22:38:22 字数 265 浏览 0 评论 0原文

有没有什么方法可以告诉 Javascript 它不应该使用除 UTC 时区之外的任何东西?

当我创建一个新的 Date 对象时,它会获取我的浏览器时区,但是当通过 JSON 传输时,这会变得混乱。

应用程序中的所有日期和时间都很幼稚,对于用户时区没有用处。因此,仅创建和使用 UTC 时间就可以了,但无论我做什么,我只能得到 UTC 日期的日期,而这还不够好。

我正在使用 Bakcbone 和 DateJS,如果这有什么区别的话。

对此有什么想法吗?

Is there some way to tell Javascript that it should never use anything but the UTC timezone?

When I create a new Date object, it gets my browsers timezone, but this will muck up when transporting via JSON.

All dates and times in the app are naive and has no use for the users timezone. So creating and working with only UTC times would be just fine, but no matter what I do, I just get what my date would look like in UTC and thats just not good enough.

I am using Bakcbone and DateJS if that makes any difference.

Any ideas on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最冷一天 2024-12-15 22:38:22

new Date().milliseconds 不传输日期的字符串表示形式。这是 UNIX 时间,ie

表示自 1 月 1 日以来的毫秒数的整数值
1970 年 00:00:00 世界标准时间。

因此与时区无关。

或者,您可以自己构建日期字符串,但使用 getUTC*< /code>方法:

var d = new Date();
alert("It's " + d.getUTCHours() + ':' + d.getUTCSeconds());

Instead of transporting the string representation of the date, new Date().milliseconds. This is the UNIX time, i.e.

Integer value representing the number of milliseconds since 1 January
1970 00:00:00 UTC.

and therefore independent of the timezone.

Alternatively, construct the date string yourself, but use the getUTC* methods:

var d = new Date();
alert("It's " + d.getUTCHours() + ':' + d.getUTCSeconds());
霓裳挽歌倾城醉 2024-12-15 22:38:22

我最终只使用 .toString() 并将其与 JSON 帖子一起发送。似乎是最简单的事情。

I ended up just using .toString() and sending that along with the JSON post. Seemed like the simplest thing to do.

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