为什么我的 javascript (node.js) 给我的时间戳不正确?
我在控制台中输入“日期”...然后得到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@KARASZI 关于根本原因是绝对正确的:Unix 时间戳始终是 UTC,除非你操纵它们。我建议,如果您想要 Unix 时间戳,则应将其保留为 UTC,并且仅在需要向用户显示格式化时间时才转换为本地时间。
这样做的第一个好处是所有服务器都可以同时“说话”。例如,如果您已将服务器部署到 Amazon EC2 美国东部和 Amazon EC2 美国西部,并且它们共享一个通用数据库,则您可以在数据库和服务器中使用 UTC 时间戳,而不必每次都担心时区转换。这是使用 UTC 时间戳的一个重要原因,但它可能不适用于您。
这样做的第二个好处是,您可以根据经过的时间来测量事物,而不必担心夏令时(或时区,如果您在移动的平台上测量时间!)。这种情况不会经常出现,但如果您遇到这样的情况:在您测量时,由于当地时间“倒退”一个小时,导致某些事情花费了负时间,您会感到非常困惑!
我能想到的第三个原因非常小,但一些性能极客会非常欣赏它:通过使用 Date 类的“now”函数,您可以获得原始 UTC 时间戳,而无需每次分配新的 Date 对象。
@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.
原因是
getTime
函数返回 UTC 时区的时间:如果要获取当前时区的 UNIX 时间戳,可以使用
getTimezoneOffset
方法:The reason is that the
getTime
function returns the time in the UTC timezone:If you want to fetch the UNIX timestamp in you current timezone, you can use the
getTimezoneOffset
method:请注意,您可以通过使用像 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.
控制台中的
date
将返回服务器时间,而在网页上使用JavaScript将返回客户端的本地时间。date
in console will return the server time, whereas using JavaScript on a webpage will return the client's local time.