最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to convert unix timestamp to JavaScript date object (consider time zone) - Stack Overflow

programmeradmin1浏览0评论
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)

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

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?

Thank you.

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)

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

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?

Thank you.

Share Improve this question edited Jul 27, 2012 at 22:47 sozhen asked Jul 25, 2012 at 18:52 sozhensozhen 7,67714 gold badges38 silver badges53 bronze badges 2
  • Yes, it's most likely using your (local) time zone. I don't know the library, but there should be a method somewhere to chose what timezone to report things in. Please note that the timestamp should not change. – Clockwork-Muse Commented Jul 25, 2012 at 18:55
  • Yes the timestamp wouldn't change. The answer for my question us great but you might also find this answer helpful. – sozhen Commented Jul 27, 2012 at 22:50
Add a comment  | 

1 Answer 1

Reset to default 19

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.

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.

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

See also

  • Annoying javascript timezone adjustment issue
发布评论

评论列表(0)

  1. 暂无评论