简体   繁体   中英

Is using timestamp to initialize Date affected by timezone?

My local machine timezone is Asia/China , UTC+8.

new Date(1509692400000).toISOString() // output: 2017-11-03T07:00:00.000Z

When I change the timezone environment variable export TZ=Europe/Amsterdam , I got the same ISO string of the date.

new Date(1509692400000).toISOString() // output: 2017-11-03T07:00:00.000Z

Does this mean using a timestamp to initialize Date , not affected by timezone?

So I can use this way to mock the Date when running the unit tests on different remote CI servers with different timezones.

As says documentation , toISOString() method always returns timezone UTC-0. In my opinion, if you want to use date in unit tests, shared in unknown-location servers, then setup Date object as you did and check date using toUTCString() - docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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