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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7481963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:15:44  来源:igfitidea点击:

How come my javascript (node.js) is giving me the incorrect timestamp?

javascriptdatenode.jstimestamp

提问by TIMEX

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

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

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

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

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

Output is: 1316505706, which is an hour behind.

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

回答by mattbornski

@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.

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

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.

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

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.

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

var ts = Date.now() / 1000;

回答by KARASZI István

The reason is that the getTimefunction returns the time in the UTC timezone:

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

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.

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

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

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

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

回答by rogierschouten

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.

请注意,您可以通过使用诸如 timezonecomplete 或 timezone-js 之类的 node.js 包来避免这种混淆,这些包的界面在日期和时间操作时不易出错。

回答by Derrick Tucker

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

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