前言

  • 让代码运行在不同国家环境下,都能准确获得指定国家的时间!

使用一个 npm 库,进行操作。

npm i @time-zone/date --save
import {isUS_Wt,isUS_Dst,getWorldTimeZone,getUTCDate} from '@time-zone/date'
isUS_Wt() // true: 美国现在是冬令时 GMT-5  ; false: 现在是夏令时
getUTCDate() // UTC 当前时间
getUTCDate("2022-2-1")
getUTCDate("2022-2-1 13:10:10")
getWorldTimeZone()
getWorldTimeZone("2022-2-1 13:10:10",'en-US')
getWorldTimeZone("2022-2-1 13:10:10",'en-US',{timeZone:'America/New_York'}) // 指定某个时区对应的时间
getWorldTimeZone(null,'en-US',{timeZone:'America/New_York'}) // 时区当前时间
getWorldTimeZone(new Date(),'en-US',{timeZone:'America/New_York'}) // 时区当前时间
getWorldTimeZone(1643692210000)
getWorldTimeZone("2022-01-15T12:00:00-00:00",'en-US',{timeZone:'America/New_York'})
getWorldTimeZone("2022-05-15T12:00:00-00:00",'en-US',{timeZone:'America/New_York'})
// "2022-05-15T12:00:00-00:00" :
// 系统这种格式如果是要 UTC时间则固定为yyyy-mm-ddThh:mm:ss-hh:mm,后面的-00:00就是当前UTC时间偏差值

getWorldTimeZone('Sat, 6/15/2022, 07:00:00 EST','en-US',{timeZone:'America/New_York'})
// 注意此处是EST时间 6月,因为是夏令时此时会返回纽约EDT时间

这个库还有其他功能: (31个API)

  • 判断美国现在是否冬令时
  • 判断美国现在是否夏令时
  • 获取指定时区的时间
    • 完整时间
    • 完整时间格式调整
    • 年月日 | 日月年 | 月日年
    • 年月日格式自定义:顺序、分隔符
    • 时分秒
    • 小时(24小时制)
    • 星期

其他API功能以及API使用的相关参数详见文档

文档地址:

https://github.com/Oumae-Kumiko/time-zone-date/blob/main/README-zh_CN.md

欢迎 star 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐