比方说,我有用户的时区
时区: 'America/New_York'
我想根据用户的时区在午夜执行一些任务。如何检查该时区是否已到达午夜?
特定时区的最后午夜:
new Date(new Date().toLocaleDateString('en-US', { timeZone: 'America/New_York', timeZoneName: 'short' } ))
特定时区最后午夜的数据库友好 UTC 时间
new Date(new Date().toLocaleDateString('en-US', { timeZone: 'America/New_York', timeZoneName: 'short' } )).toISOString()
注意:将“最后午夜”理解为今天区内午夜
解释:
-
new Date()
是当前时间(无论区域 - 时间对象如何)
-
.toLocaleDateString('en-US', { timeZone: 'America/New_York', timeZoneName: 'short' } )
显示指定区域中的当前日期(没有时间,因此午夜)(带有区域引用的时间字符串)
-
new Date(<above midnight time in zone>)
解析zone中的午夜时间并显示对应的当前时区时间(time对象)
-
.toISOString()
在 UTC 参考上方显示(ISO 日期时间字符串)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)