简体   繁体   English

如何将unix时间戳转换为JavaScript日期对象(考虑时区)

[英]How to convert unix timestamp to JavaScript date object (consider time zone)

var date = new Date(1257397200000​);
document.write(date);
​

Ran the code above I got Wed Nov 04 2009 23:00:00 GMT-0600 (Central Standard Time) 跑了上面的代码我得到了Wed Nov 04 2009 23:00:00 GMT-0600 (Central Standard Time)

I am looking for a way to create date object based on different time zone, say for that time stamp I want to obtain date object like Thursday, November 5th 2009, 00:00:00 (GMT -5) . 我正在寻找一种基于不同时区创建日期对象的方法,比如我希望获得日期对象的时间戳,如Thursday, November 5th 2009, 00:00:00 (GMT -5)

Note that the dates are different according to above two time zones, though they represent same point in time . 请注意,根据上述两个时区,日期不同, 但它们代表相同的时间点 I am in CST, is that why the created object is generated using CST? 我在CST,是为什么使用CST生成创建的对象?

Thank you. 谢谢。

No, these dates aren't different as they don't represent different point in time. 不,这些日期没有不同,因为它们不代表不同的时间点。 The both represent Thu, 05 Nov 2009 05:00:00 GMT . 这两个代表Thu, 05 Nov 2009 05:00:00 GMT

Date object in JavaScript is time-zone independent, it only represents point in time. JavaScript中的Date对象与时区无关,它仅代表时间点。 The fact that Date.toString() includes time zone is very misleading, there is no time-zone information in Date . Date.toString()包含时区的事实非常具有误导性, Date没有时区信息 It is only a wrapper around milliseconds since epoch. 它只是一个自epoch以来几毫秒的包装器。

The time zone you see is based on OS/browser locale. 您看到的时区基于操作系统/浏览器区域设置。 You cannot create Date object in different time-zone . 您不能在不同的时区创建Date对象 Consider using getUTC*() family of methods to get browser time-zone agnostic values. 考虑使用getUTC*()系列方法来获取浏览器时区不可知值。

BTW your example code prints: BTW您的示例代码打印:

Thu Nov 05 2009 06:00:00 GMT+0100 (CET)

on my computer - and this is still the same point in time. 在我的电脑上 - 这仍然是同一时间点。

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM