简体   繁体   English

是否使用时间戳来初始化受时区影响的日期?

[英]Is using timestamp to initialize Date affected by timezone?

My local machine timezone is Asia/China , UTC+8.我的本地机器时区是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.当我更改时区环境变量export TZ=Europe/Amsterdam时,我得到了相同的 ISO 日期字符串。

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

Does this mean using a timestamp to initialize Date , not affected by timezone?这是否意味着使用时间戳来初始化Date ,不受时区影响?

So I can use this way to mock the Date when running the unit tests on different remote CI servers with different timezones.因此,在具有不同时区的不同远程 CI 服务器上运行单元测试时,我可以使用这种方式来模拟日期。

As says documentation , toISOString() method always returns timezone UTC-0.文档所述, toISOString()方法始终返回时区 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在我看来,如果你想在单元测试中使用日期,在未知位置的服务器中共享,然后像你一样设置Date object 并使用toUTCString()检查日期 - 文档

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

相关问题 将日期和时间戳与时区进行比较 - Compare date and timestamp with timezone 如何使用客户端时区在客户端中输出数据库时间戳作为日期戳? - How to output a db timestamp as a date stamp in the client using the clients timezone? 如何使用date-fns正确格式化带有时区日期的postgres时间戳? - How to correctly format a postgres timestamp with timezone date using date-fns? 将postgres时间戳(+时区)转换为Date对象[Javascript] - Convert postgres timestamp (+ timezone) to Date object [Javascript] 将带有时区的unix时间戳转换为javascript日期 - Convert unix timestamp with a timezone to javascript date 使用Date()转换时区的指定偏移量的时间戳; - Convert timestamp with specified offset for timezone with Date(); 从Unix时间戳创建日期忽略时区 - Create date from Unix Timestamp ignoring timezone 从没有时区和时区名称的日期字符串中获取时间戳 - Get timestamp from date string without timezone and timezone name apart 是否可以使用 GMT 偏移量作为 moment.js 中的时区将时间戳转换为日期和时间? - Is it possible to convert timestamp to date and time using GMT offset as timezone in moment.js? 使用Moment时区格式化指定时区中的日期 - Formatting date in specified timezone using Moment Timezone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM