如何将unix时间戳转换为JavaScript日期对象(考虑时区) [英] How to convert unix timestamp to JavaScript date object (consider time zone)
问题描述
var date = new Date(1257397200000);
document.write(date);
上面的代码我得到 Wed Nov 04 2009 23:00:00 GMT-0600(中央标准时间)
我正在寻找一种基于不同时区创建日期对象的方法对于那个时间戳,我想要获取日期对象,如 2009年11月5日星期四00:00:00(GMT -5)
。
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)
.
请注意,根据上述两个时区,日期不同,虽然它们代表相同的时间点。我在CST中,是为什么使用CST生成创建的对象?
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?
谢谢。
推荐答案
不,这些日期并不相同,因为它们不代表不同的时间点。两者代表 Thu,05 Nov 2009 05:00:00 GMT
。
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
.
日期JavaScript中的
对象与时区无关,仅代表时间点。事实上, Date.toString()
包含时区是非常误导的, Date $ c中没有时区信息$ c>
。从epoch开始,它只是几毫秒的包装。
Date
object in JavaScript is time-zone independent, it only represents point in time. The fact that Date.toString()
includes time zone is very misleading, there is no time-zone information in Date
. It is only a wrapper around milliseconds since epoch.
您看到的时区是基于OS /浏览器区域设置的。 您不能在不同的时区中创建 Date
对象。考虑使用 getUTC *()
方法系列获取浏览器时区不可知值。
The time zone you see is based on OS/browser locale. You cannot create Date
object in different time-zone. Consider using getUTC*()
family of methods to get browser time-zone agnostic values.
BTW您的示例代码打印:
BTW your example code prints:
Thu Nov 05 2009 06:00:00 GMT+0100 (CET)
在我的电脑上 - 这仍然是同一时间点。
on my computer - and this is still the same point in time.
- Annoying javascript timezone adjustment issue
这篇关于如何将unix时间戳转换为JavaScript日期对象(考虑时区)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!