为什么我的 javascript (node.js) 给我的时间戳不正确?

发布于 2024-12-05 11:27:16 字数 234 浏览 0 评论 0原文

我在控制台中输入“日期”...然后得到 Tue Sep 20 01:01:49 PDT 2011 ...这是正确的。

但后来我在 Node.js 中执行此操作,但得到了错误的时间。

 var ts = String(Math.round(new Date().getTime() / 1000));

输出为:1316505706,晚了一个小时。

I typed "date" in console...and I get Tue Sep 20 01:01:49 PDT 2011 ...which is correct.

But then I do this in node.js, and I get the wrong time.

 var ts = String(Math.round(new Date().getTime() / 1000));

Output is: 1316505706, which is an hour behind.

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

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

发布评论

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

评论(4

ぺ禁宫浮华殁 2024-12-12 11:27:16

@KARASZI 关于根本原因是绝对正确的:Unix 时间戳始终是 UTC,除非你操纵它们。我建议,如果您想要 Unix 时间戳,则应将其保留为 UTC,并且仅在需要向用户显示格式化时间时才转换为本地时间。

这样做的第一个好处是所有服务器都可以同时“说话”。例如,如果您已将服务器部署到 Amazon EC2 美国东部和 Amazon EC2 美国西部,并且它们共享一个通用数据库,则您可以在数据库和服务器中使用 UTC 时间戳,而不必每次都担心时区转换。这是使用 UTC 时间戳的一个重要原因,但它可能不适用于您。

这样做的第二个好处是,您可以根据经过的时间来测量事物,而不必担心夏令时(或时区,如果您在移动的平台上测量时间!)。这种情况不会经常出现,但如果您遇到这样的情况:在您测量时,由于当地时间“倒退”一个小时,导致某些事情花费了负时间,您会感到非常困惑!

我能想到的第三个原因非常小,但一些性能极客会非常欣赏它:通过使用 Date 类的“now”函数,您可以获得原始 UTC 时间戳,而无需每次分配新的 Date 对象。

var ts = Date.now() / 1000;

@KARASZI is absolutely correct about the root cause: Unix timestamps are always UTC unless you manipulate them. I would suggest that if you want a Unix timestamp you should leave it in UTC, and only convert to local time if you need to display a formatted time to the user.

The first benefit of doing this is that all your servers can "speak" the same time. For instance, if you've deployed servers to Amazon EC2 US East and Amazon EC2 US West and they share a common database, you can use UTC timestamps in your database and on your servers without worrying about timezone conversions every time. This is a great reason to use UTC timestamps, but it might not apply to you.

The second benefit of this is that you can measure things in terms of elapsed time without having to worry about daylight savings time (or timezones either, in case you're measuring time on a platform which is moving!). This doesn't come up very much, but if you had a situation where something took negative time because the local time "fell back" an hour while you were measuring, you'd be very confused!

The third reason I can think of is very minor, but some performance geeks would really appreciate it: you can get the raw UTC timestamp without allocating a new Date object each time, by using the Date class's "now" function.

var ts = Date.now() / 1000;
趁微风不噪 2024-12-12 11:27:16

原因是 getTime 函数返回 UTC 时区的时间:

getTime 方法返回的值是自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。您可以使用此方法帮助将日期和时间分配给另一个 Date 对象。

如果要获取当前时区的 UNIX 时间戳,可以使用 getTimezoneOffset 方法:

var date = new Date();
var ts = String(Math.round(date.getTime() / 1000) + date.getTimezoneOffset() * 60);

The reason is that the getTime function returns the time in the UTC timezone:

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC. You can use this method to help assign a date and time to another Date object.

If you want to fetch the UNIX timestamp in you current timezone, you can use the getTimezoneOffset method:

var date = new Date();
var ts = String(Math.round(date.getTime() / 1000) + date.getTimezoneOffset() * 60);
执手闯天涯 2024-12-12 11:27:16

请注意,您可以通过使用像 timezonecomplete 或 timezone-js 这样的 Node.js 包来避免这种混乱,它们的接口在日期和时间操作方面更不容易出错。

Note you can avoid this confusion by using a node.js package like timezonecomplete or timezone-js which have an interface that is much less error-prone for date and time manipulation.

把回忆走一遍 2024-12-12 11:27:16

控制台中的date将返回服务器时间,而在网页上使用JavaScript将返回客户端的本地时间。

date in console will return the server time, whereas using JavaScript on a webpage will return the client's local time.

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