javascript 获取指定国家(时区)的时间 - npm库
让代码运行在不同国家环境下,都能准确获得指定国家、时区的时间!判断美国是否冬令时、夏令时,能自定义返回时间的格式等其他API。完整时间、完整时间格式调整、年月日 | 日月年 | 月日年、年月日格式自定义:顺序、分隔符、时分秒、年、月、日、小时(24小时制)、分、秒、星期
·
前言
- 让代码运行在不同国家环境下,都能准确获得指定国家的时间!
使用一个 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
更多推荐
所有评论(0)