├── README-zh_CN.md
├── README.md
├── data.ts
├── index.ts
├── operation.ts
├── package.json
├── time-zone.ts
└── tsconfig.json
/README-zh_CN.md:
--------------------------------------------------------------------------------
1 | # 获取世界各时区、时间的工具库
2 |
3 |
4 | [](https://www.npmjs.com/package/@time-zone/date)
5 | [](https://github.com/Oumae-Kumiko/time-zone-date)
6 | [](https://github.com/Oumae-Kumiko/time-zone-date/issues)
7 | [](https://www.npmjs.com/package/@time-zone/date)
8 |
9 |
10 | [English](https://github.com/Oumae-Kumiko/time-zone-date/blob/main/README.md) | 简体中文
11 |
12 |
13 |
14 | ## 前言
15 | * 让代码运行在不同国家环境下,都能准确获得指定国家的时间!
16 |
17 |
18 | ## 安装
19 | ```bash
20 | npm i @time-zone/date --save
21 | ```
22 |
23 |
24 | ## 例子
25 | * 所有 API 引入方式都一样
26 | * 总共 31 个API
27 | ```js
28 | import {isUS_Wt,isUS_Dst,getWorldTimeZone,getUTCDate} from '@time-zone/date'
29 | isUS_Wt() // true: 美国现在是冬令时 GMT-5 ; false: 现在是夏令时
30 | getUTCDate() // UTC 当前时间
31 | getUTCDate("2022-2-1")
32 | getUTCDate("2022-2-1 13:10:10")
33 | getWorldTimeZone()
34 | getWorldTimeZone("2022-2-1 13:10:10",'en-US')
35 | getWorldTimeZone("2022-2-1 13:10:10",'en-US',{timeZone:'America/New_York'}) // 指定某个时区对应的时间
36 | getWorldTimeZone(null,'en-US',{timeZone:'America/New_York'}) // 时区当前时间
37 | getWorldTimeZone(new Date(),'en-US',{timeZone:'America/New_York'}) // 时区当前时间
38 | getWorldTimeZone(1643692210000)
39 | ```
40 |
41 |
42 | # 功能介绍
43 | * 判断美国现在是否冬令时
44 | * 判断美国现在是否夏令时
45 |
46 | - 获取指定时区的时间
47 | * 完整时间
48 | * 完整时间格式调整
49 | * 年月日 | 日月年 | 月日年
50 | * 年月日格式自定义:顺序、分隔符
51 | * 时分秒
52 | * 年
53 | * 月
54 | * 日
55 | * 小时(24小时制)
56 | * 分
57 | * 秒
58 | * 星期
59 |
60 |
61 |
62 | > 额外提供,用上面的方法可以替代下面所有内容
63 | * 获取EST时区时间
64 | * 完整时间
65 | * 月日年
66 | * 时分秒
67 | * 年
68 | * 月
69 | * 日
70 | * 小时(24小时制,可设置0为新一天起点或24为新一天起点)
71 | * 分
72 | * 秒
73 | * 星期
74 |
75 |
76 | * 获取UTC时区时间:时差±0
77 | 1. 完整时间
78 | 2. 年
79 | 3. 月
80 | 4. 日
81 | 5. 时
82 | 6. 分
83 | 7. 秒
84 |
85 |
86 |
87 | # API
88 |
89 | ## getWorldTimeZone()
90 | > 如介绍的开头内容,这是主要 API ,`locales` 和 `options` 参数参考见文档末尾
91 | - 获取指定时区的时间
92 | * 参数3个:
93 | * `date`
94 | * 用途:`时间参数;以当前运行的系统环境为基准,获取对应时区的时间`
95 | * 类型:`?:string | number | Date | null | undefined`
96 | * 值: `符合new Date()第一个参数的都可以使用,null 和 undefined表示当前时间。`
97 | * `locales`
98 | * 用途:`返回对应的语言类型`
99 | * 类型:`?:string`
100 | * 值: `language[-scripts][-region]`
101 | * `options`
102 | * 用途:`调整返回的时间格式内容,指定返回时区的时间`
103 | * 类型:`?:object`
104 | * 值: `详情参数查看下方 options 内容`
105 | ```javascript
106 | import { getWorldTimeZone } from '@time-zone/date'
107 | // 获取目标时区完整时间
108 | let date = getWorldTimeZone(new Date(),'en-US',{timeZone:'America/New_York'})
109 |
110 | // 不传则默认代码运行环境时的当前时区
111 | // getWorldTimeZone()
112 | ```
113 |
114 |
115 |
116 | ## custom_yyyymmdd_sort()
117 | 获取指定时区的年月日,并且可自定义年月日的顺序(月日为个位数的情况下补充0)
118 | * 参数4个
119 | * `timeZone`:类型 `string`
120 | * 年月日顺序:类型 `string`
121 | * 1、必须为小写。2、字母为`y`、`m`、`d`。3、不传默认yyyymmdd
122 | * 3个字母的顺序决定返回的顺序
123 | * 分隔符,不传默认为`/`
124 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
125 | ```javascript
126 | import { custom_yyyymmdd_sort } from '@time-zone/date'
127 | let ymd = custom_yyyymmdd_sort('Asia/Shanghai','ymd','-', new Date())
128 | let dmy = custom_yyyymmdd_sort('Asia/Shanghai','dmy','/')
129 | let ydm = custom_yyyymmdd_sort('Asia/Shanghai','ydm','*-@#$$%^^*&(*&)(_+`~')
130 | let mdy = custom_yyyymmdd_sort('Asia/Shanghai','mdy')
131 |
132 | // 年月日可任意组合,并且如果出现重复的,返回结果也会重复出现
133 | let ymdy = custom_yyyymmdd_sort('Asia/Shanghai','ymdy')
134 | ```
135 |
136 |
137 |
138 | ## get_mmddyyyy() get_ddmmyyyy() get_yyyymmdd()
139 | 获取指定时区的月日年(月日为个位数的情况下补充0)
140 | * 参数3个
141 | * `timeZone`:类型 `string`;
142 | * 分隔符:不传默认为`/`;
143 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
144 | ```javascript
145 | import { get_mmddyyyy,get_ddmmyyyy,get_yyyymmdd } from '@time-zone/date'
146 | let mdy = get_mmddyyyy('Asia/Shanghai','/', new Date())
147 | // get_ddmmyyyy 和 get_yyyymmdd 不解释了,看名字就知道顺序了。
148 |
149 | ```
150 |
151 |
152 |
153 | ## custom_ymd_sort()
154 | - 月日为个位数的情况下不会补充0
155 | - 其他内容跟 `custom_yyyymmdd_sort()` 一致,不再解释
156 | ```javascript
157 | import { custom_ymd_sort } from '@time-zone/date'
158 | let ymd = custom_ymd_sort( 'Asia/Shanghai','ymd','-',new Date() )
159 | ```
160 |
161 |
162 |
163 | ## get_hms()
164 | - 获取目标时区的时分秒
165 | * 参数2个:
166 | * `timeZone`:类型 `string`
167 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
168 | ```javascript
169 | import { get_hms } from '@time-zone/date'
170 | let hms = get_hms('Asia/Shanghai') // 格式:08:00:00
171 | ```
172 |
173 |
174 | ## getYear()
175 | - 获取目标时区的年
176 | * 参数2个:
177 | * `timeZone`:类型 `string`
178 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
179 | ```javascript
180 | import { getYear } from '@time-zone/date'
181 | let year = getYear('Asia/Shanghai') // 返回值 number 类型
182 | ```
183 |
184 |
185 | ## getMonth()
186 | - 获取目标时区的月(0-11)
187 | * 参数2个:
188 | * `timeZone`:类型 `string`
189 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
190 | ```javascript
191 | import { getMonth } from '@time-zone/date'
192 | let month = getMonth('Asia/Shanghai') // 返回值 number 类型
193 | ```
194 |
195 |
196 | ## getDate()
197 | - 获取目标时区的日
198 | * 参数2个:
199 | * `timeZone`:类型 `string`
200 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
201 | ```javascript
202 | import { getDate } from '@time-zone/date'
203 | let date = getDate('Asia/Shanghai') // 返回值 number 类型
204 | ```
205 |
206 |
207 | ## getHours()
208 | - 获取目标时区的小时
209 | - 单独获取目标时区的小时,统一返回值为:`0-23`,避免因美国1-24的问题造成混乱。
210 | * 参数2个:
211 | * `timeZone`:类型 `string`
212 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
213 | ```javascript
214 | import { getHours } from '@time-zone/date'
215 | let hours = getHours('Asia/Shanghai') // 返回值 number 类型
216 | ```
217 |
218 |
219 | ## getMinutes()
220 | - 获取目标时区的分钟
221 | * 参数2个:
222 | * `timeZone`:类型 `string`
223 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
224 | ```javascript
225 | import { getMinutes } from '@time-zone/date'
226 | let minutes = getMinutes('Asia/Shanghai') // 返回值 number 类型
227 | ```
228 |
229 |
230 | ## getSeconds()
231 | - 获取目标时区的秒
232 | * 参数2个:
233 | * `timeZone`:类型 `string`
234 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
235 | ```javascript
236 | import { getSeconds } from '@time-zone/date'
237 | let sec = getSeconds('Asia/Shanghai') // 返回值 number 类型
238 | ```
239 |
240 |
241 | ## getDay()
242 | - 获取目标时区的星期
243 | - 返回值为 `0-6` ,0为星期天,1为星期一,后面依次排序
244 | * 参数2个:
245 | * `timeZone`:类型 `string`
246 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
247 | ```javascript
248 | import { getDay } from '@time-zone/date'
249 | let day = getDay('Asia/Shanghai') // 返回值 number 类型
250 | ```
251 |
252 |
253 |
254 |
255 | ## getESTDate()
256 | 获取EST时区的时间,返回的语言类型en-US
257 | * 参数1个:
258 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
259 | ```javascript
260 | import { getESTDate } from '@time-zone/date'
261 | let date = getESTDate()
262 | ```
263 |
264 |
265 |
266 | ## getUTCDate()
267 | 获取UTC时区时间
268 | * 参数1个:
269 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
270 | ```javascript
271 | import { getUTCDate } from '@time-zone/date'
272 | let date = getUTCDate()
273 | ```
274 |
275 |
276 |
277 | ## isUS_Dst()
278 | 判断美国现在是否夏令时
279 | * 无参数,返回 `true` 表示美国当前是夏令时
280 | * 返回值 `true` | `false`
281 | ```javascript
282 | import { isUS_Dst } from '@time-zone/date'
283 | let booleanDate = isUS_Dst()
284 | ```
285 |
286 |
287 |
288 | ## isUS_Wt()
289 | 判断美国现在是否冬令时
290 | * 无参数,返回 `true` 表示美国当前是冬令时
291 | * 返回值 `true` | `false`
292 | ```javascript
293 | import { isUS_Wt } from '@time-zone/date'
294 | let booleanDate = isUS_Wt()
295 | ```
296 |
297 |
298 |
299 | # 其他
300 | ## EST时间的对应值
301 | * 参数1个:
302 | * 时间参数;以当前运行的系统环境为基准,获取对应时区的时间; 参数类型为 `?:string | number | Date | null | undefined`
303 | ```javascript
304 | import { getESTmmddyyyy, ...... } from '@time-zone/date'
305 | getESTmmddyyyy() // 月日年
306 | getESThms() // 时分秒
307 | EST_getYear() // 年
308 | EST_getMonth() // 月
309 | EST_getDate() // 日
310 | EST_getHours(0) // 美国小时,如果传0表示新的一天起点为0,否则为24
311 | EST_getMinutes() // 分
312 | EST_getSeconds() // 秒
313 | EST_getDay() // 星期
314 | ```
315 |
316 |
317 |
318 |
319 | # `locales` 参数
320 | * 类型:`string`
321 | * language[-scripts][-region]
322 | - 特殊编码可传入对应的编码类型替换,具体参考MDN网站
323 | * language[-scripts][-region]-u-nu-*
324 | * language[-scripts][-region]-u-ca-*
325 |
326 |
327 |
328 | - 参考值
329 | - 其他值请参考:http://www.lingoes.cn/zh/translator/langcode.htm
330 | ```javascript
331 | "zh-Hans" //简体中文
332 | "zh-TW" //繁体中文
333 | "ru-RU" //俄语
334 | "en-GB" //英国
335 | "it-IT" //意大利
336 | "fr-FR" //法语
337 | "pt-PT" //葡萄牙
338 | "de-DE" //德语
339 | "en-US" //美国
340 | "en-AU" //澳大利亚
341 | "en-CA" //加拿大
342 | "ko-KR" //韩国
343 | "ar-EG" //阿拉伯
344 | "ja-JP" //日本
345 | "nl-NL" //荷兰
346 | "es-ES" //西班牙
347 | "hi-IN" //印地语
348 | "eo" //世界语
349 | ```
350 |
351 |
352 |
353 | # `options` 参数
354 | * 类型:`object`
355 |
356 | * 内置默认参数为
357 | ```javascript
358 | const DEFAULT_OPTIONS = {
359 | year: "numeric",
360 | month: "numeric",
361 | day: "numeric",
362 | hour: "numeric",
363 | minute: "numeric",
364 | second: "numeric",
365 | weekday: "short",
366 | hour12:false,
367 | timeZoneName :"short",
368 | }
369 | ```
370 |
371 | ### 可配置参数,如跟默认参数有相同的属性,则会以传入的参数为主
372 | ```javascript
373 | let options = {
374 | era: "long", //是否展示公元:"narrow", "short", "long" 依次为极短、短、长(完整)
375 | year: "numeric", //"numeric" 正常数值表示, "2-digit" 显示两位数
376 | month: "numeric", //"numeric", "2-digit", "narrow", "short", "long"
377 | day: "numeric", //"numeric", "2-digit"
378 | hour: "numeric", //"numeric", "2-digit"
379 | minute: "numeric",//"numeric", "2-digit"
380 | second: "numeric",//"numeric", "2-digit"
381 | weekday: "long", //"narrow", "short", "long"
382 | hour12:false, // true | false
383 | timeZoneName :"short", //"short", "long"
384 | timeZone:"", // 指定城市的时区
385 | }
386 | ```
387 | ---
388 |
389 | # `options.timeZone` 的参考值:
390 | - 其他值请参考:https://www.iana.org/time-zones
391 | ```javascript
392 | timeZone:'UTC' // UTC 时间
393 | timeZone:'Europe/Berlin' // GMT+1 德国时区
394 | timeZone:'Australia/Sydney' // GMT+11 澳大利亚时区
395 | timeZone:'Europe/Moscow' // GMT+3 俄罗斯时区
396 | timeZone:'Europe/Paris' // GMT+1 法国时区
397 | timeZone:'Europe/London' // GMT 英国时区
398 | timeZone:'Asia/Tokyo' // JST 东京时区
399 | timeZone:'Asia/Shanghai' // GMT+8 北京时区
400 | timeZone:'America/Denver' // MST 美国山地时区
401 | timeZone:'America/Los_Angeles' // PTZ 太平洋时区
402 | timeZone:'America/New_York' // EST 美国东部时区
403 | timeZone:'America/Chicago' // CST 美国中部时区
404 | ```
405 |
406 |
407 |
408 |
409 |
410 |
411 | # 版本更新历史
412 |
413 | ## v2.2.2
414 | 更新文档参数说明
415 |
416 | ## v2.2.1
417 | 更新文档例子参考
418 |
419 | ## v2.2.0
420 | 所有API增加可指定对应时间
421 |
422 | ## v2.1.2
423 | 验证开发、生产环境通过
424 |
425 | ## v2.1.1
426 | 增加tsconfig.json
427 |
428 | ## v2.1.0
429 | 将库的 JS 全改为 TS
430 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Get the tool library of time zones and times in the world
2 |
3 | ## Preface
4 | * Let the code run in different national environments, you can accurately obtain the time of the specified country!
5 |
6 | [](https://www.npmjs.com/package/@time-zone/date)
7 | [](https://github.com/Oumae-Kumiko/time-zone-date)
8 | [](https://github.com/Oumae-Kumiko/time-zone-date/issues)
9 |
10 | English | [简体中文](https://github.com/Oumae-Kumiko/time-zone-date/blob/main/README-zh_CN.md)
11 |
12 | ## Installation
13 | ```bash
14 | npm i @time-zone/date --save
15 | ```
16 |
17 |
18 | ## Example
19 | * All APIs are introduced in the same way
20 | * Total 31 APIs
21 | ```js
22 | import {isUS_Wt,isUS_Dst,getWorldTimeZone,getUTCDate} from '@time-zone/date'
23 | isUS_Wt() // true: It's winter time in the United States GMT-5 ; false: You know...
24 | getUTCDate() // UTC Current time
25 | getUTCDate("2022-2-1")
26 | getUTCDate("2022-2-1 13:10:10")
27 | getWorldTimeZone()
28 | getWorldTimeZone("2022-2-1 13:10:10",'en-US')
29 | getWorldTimeZone("2022-2-1 13:10:10",'en-US',{timeZone:'America/New_York'}) // Specify the time corresponding to a time zone
30 | getWorldTimeZone(null,'en-US',{timeZone:'America/New_York'}) // Time zone current time
31 | getWorldTimeZone(new Date(),'en-US',{timeZone:'America/New_York'}) // Time zone current time
32 | getWorldTimeZone(1643692210000)
33 | ```
34 |
35 |
36 | # Referral function
37 | * Determine whether the United States is now in winter time
38 | * Judge whether the United States is now daylight saving time
39 |
40 | * Gets the time in the specified time zone
41 | * Full time
42 | * Full time format adjustment
43 | * yyyymmdd | ddmmyyyy | mmddyyyy
44 | * Custom year and month and date formats: order, separator
45 | * hhmmss
46 | * year
47 | * month
48 | * date
49 | * hours(24-hour system)
50 | * minutes
51 | * seconds
52 | * week
53 |
54 |
55 |
56 | > In addition, the above method can replace all the following contents
57 | * Get EST time zone
58 | * Full time
59 | * yyyymmdd
60 | * hhmmss
61 | * yearv
62 | * month
63 | * date
64 | * hours(24-hour system. You can set 0 as the starting point of a new day or 24 as the starting point of a new day)
65 | * minutes
66 | * seconds
67 | * week
68 |
69 |
70 | * Get UTC time zone: time difference ± 0
71 | * Full time
72 | * year
73 | * month
74 | * date
75 | * hours
76 | * minutes
77 | * seconds
78 |
79 |
80 |
81 | # API
82 |
83 | ## getWorldTimeZone()
84 | > As mentioned at the beginning of the introduction, this is the main API .
85 | > See the end of the document for references to `locales` and `options` parameters.
86 | - Gets the time in the specified time zone.
87 | * 3 parameters
88 | * `date`
89 | * purpose:`Time parameters; Obtain the time of the corresponding time zone based on the current running system environment.`
90 | * type:`?:string | number | Date | null | undefined`
91 | * value: `Those that match the first parameter of 'new date()' can be used, and 'null' and 'undefined' represent the current time.`
92 | * `locales`
93 | * purpose:`Returns the corresponding language type`
94 | * type:`?:string`
95 | * value: `language[-scripts][-region]`
96 | * `options`
97 | * purpose:`Adjust the returned time format content. Specifies the time to return to the time zone`
98 | * type:`?:object`
99 | * value: `See options parameter below`
100 | ```javascript
101 | import { getWorldTimeZone } from '@time-zone/date'
102 | // Get the full time of the target time zone
103 | let date = getWorldTimeZone(new Date(),'en-US',{timeZone:'America/New_York'})
104 |
105 | // If it is not transmitted, the current time zone of the code running environment will be the default
106 | // getWorldTimeZone()
107 | ```
108 |
109 |
110 |
111 | ## custom_yyyymmdd_sort()
112 | Get the month, year and day of the specified time zone, and you can customize the order of month, year and day (supplement 0 when month and day are single digits)
113 | * 4 parameters
114 | * `timeZone`:type `string`
115 | * Date sequence:type `string`
116 | * 1、Must be lowercase。
117 | * 2、The letter is : `y`、`m`、`d`。
118 | * 3、The default value for parameters not transmitted isyyyymmdd
119 | * The order of 3 letters determines the order of return
120 | * Separator, not passed, default to `/`
121 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
122 | ```javascript
123 | import { custom_yyyymmdd_sort } from '@time-zone/date'
124 | let ymd = custom_yyyymmdd_sort('Asia/Shanghai','ymd','-',new Date())
125 | let dmy = custom_yyyymmdd_sort('Asia/Shanghai','dmy','/')
126 | let ydm = custom_yyyymmdd_sort('Asia/Shanghai','ydm','*-@#$$%^^*&(*&)(_+`~')
127 | let mdy = custom_yyyymmdd_sort('Asia/Shanghai','mdy')
128 |
129 | // The month, year and day can be combined arbitrarily, and if there is a repetition, the returned result will also appear repeatedly
130 | let ymdy = custom_yyyymmdd_sort('Asia/Shanghai','ymdy')
131 | ```
132 |
133 |
134 |
135 | ## get_mmddyyyy() get_ddmmyyyy() get_yyyymmdd()
136 | Get the month, day and year of the specified time zone (supplement 0 if the month and day are single digits)
137 | * 3 Parameters
138 | * `timeZone`:type `string`
139 | * Separator, not passed, default to `/`
140 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
141 | ```javascript
142 | import { get_mmddyyyy,get_ddmmyyyy,get_yyyymmdd } from '@time-zone/date'
143 | let mdy = get_mmddyyyy('Asia/Shanghai','/', new Date())
144 | // get_ddmmyyyy() , get_yyyymmdd()
145 | // No explanation, just look at the name to know the order.
146 |
147 | ```
148 |
149 |
150 |
151 | ## custom_ymd_sort()
152 | - 0 will not be added if the month day is a single digit
153 | - Other contents follow ` custom_ yyyymmdd_ Sort() ` consistent, no further explanation
154 | ```javascript
155 | import { custom_ymd_sort } from '@time-zone/date'
156 | let ymd = custom_ymd_sort( 'Asia/Shanghai','ymd','-',new Date() )
157 | ```
158 |
159 |
160 |
161 | ## get_hms()
162 | - Gets the hour, minute, and second of the target time zone
163 | * 2 parameters
164 | * `timeZone`:type `string`
165 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
166 | ```javascript
167 | import { get_hms } from '@time-zone/date'
168 | let hms = get_hms('Asia/Shanghai') // format:08:00:00
169 | ```
170 |
171 |
172 | ## getYear()
173 | - Gets the year of the target time zone
174 | * 2 parameters
175 | * `timeZone`:type `string`
176 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
177 | ```javascript
178 | import { getYear } from '@time-zone/date'
179 | let year = getYear('Asia/Shanghai') // Return value number type
180 | ```
181 |
182 |
183 | ## getMonth()
184 | - Gets the month (0-11) of the target time zone
185 | * 2 parameters
186 | * `timeZone`:type `string`
187 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
188 | ```javascript
189 | import { getMonth } from '@time-zone/date'
190 | let month = getMonth('Asia/Shanghai') // Return value number type
191 | ```
192 |
193 |
194 | ## getDate()
195 | - Gets the day of the target time zone
196 | * 2 parameters
197 | * `timeZone`:type `string`
198 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
199 | ```javascript
200 | import { getDate } from '@time-zone/date'
201 | let date = getDate('Asia/Shanghai') // Return value number type
202 | ```
203 |
204 |
205 | ## getHours()
206 | - Gets the hour of the target time zone
207 | - Get the hours of the target time zone separately, and the unified return value is: '0-23', so as to avoid confusion caused by the problem of 1-24 in the United States.
208 | * 2 parameters
209 | * `timeZone`:type `string`
210 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
211 | ```javascript
212 | import { getHours } from '@time-zone/date'
213 | let hours = getHours('Asia/Shanghai') // Return value number type
214 | ```
215 |
216 |
217 | ## getMinutes()
218 | - Gets the minutes of the target time zone
219 | * 2 parameters
220 | * `timeZone`:type `string`
221 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
222 | ```javascript
223 | import { getMinutes } from '@time-zone/date'
224 | let minutes = getMinutes('Asia/Shanghai') // Return value number type
225 | ```
226 |
227 |
228 | ## getSeconds()
229 | - Gets the second of the target time zone
230 | * 2 parameters
231 | * `timeZone`:type `string`
232 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
233 | ```javascript
234 | import { getSeconds } from '@time-zone/date'
235 | let sec = getSeconds('Asia/Shanghai') // Return value number type
236 | ```
237 |
238 |
239 | ## getDay()
240 | - Gets the week in the target time zone
241 | - The return value is' 0-6 ', 0 is Sunday, 1 is Monday, followed by order
242 | * 2 parameters
243 | * `timeZone`:type `string`
244 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
245 | ```javascript
246 | import { getDay } from '@time-zone/date'
247 | let day = getDay('Asia/Shanghai') // Return value number type
248 | ```
249 |
250 |
251 |
252 |
253 | ## getESTDate()
254 | * Get the time of EST time zone. The returned language type is en US
255 | * parameter
256 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
257 | ```javascript
258 | import { getESTDate } from '@time-zone/date'
259 | let date = getESTDate()
260 | ```
261 |
262 |
263 |
264 | ## getUTCDate()
265 | * Get UTC time zone
266 | * parameter
267 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
268 | ```javascript
269 | import { getUTCDate } from '@time-zone/date'
270 | let date = getUTCDate()
271 | ```
272 |
273 |
274 |
275 | ## isUS_Dst()
276 | Judge whether the United States is now daylight saving time
277 | * No parameters.
278 | * Returning 'true' indicates that the United States is currently daylight saving time
279 | * Return value: `true` | `false`
280 | ```javascript
281 | import { isUS_Dst } from '@time-zone/date'
282 | let booleanDate = isUS_Dst()
283 | ```
284 |
285 |
286 |
287 | ## isUS_Wt()
288 | Determine whether the United States is now in winter time
289 | * No parameters.
290 | * Returning 'true' indicates that it is winter time in the United States
291 | * Return value: `true` | `false`
292 | ```javascript
293 | import { isUS_Wt } from '@time-zone/date'
294 | let booleanDate = isUS_Wt()
295 | ```
296 |
297 |
298 |
299 | # Other
300 | ## Corresponding value of EST time
301 | * parameter
302 | * Time parameters; Obtain the time of the corresponding time zone based on the current running system environment. Parameter type is `?:string | number | Date | null | undefined`
303 | ```javascript
304 | import { getESTmmddyyyy, ...... } from '@time-zone/date'
305 | getESTmmddyyyy()
306 | getESThms()
307 | EST_getYear()
308 | EST_getMonth()
309 | EST_getDate()
310 | EST_getHours(0) // U.S. hours. If 0 is passed, it means that the starting point of the new day is 0, otherwise it is 24
311 | EST_getMinutes()
312 | EST_getSeconds()
313 | EST_getDay()
314 | ```
315 |
316 |
317 |
318 |
319 | # `locales` parameter
320 | * type:`string`
321 | * language[-scripts][-region]
322 | - The special code can be replaced by the corresponding code type. Refer to the MDN website for details
323 | * language[-scripts][-region]-u-nu-*
324 | * language[-scripts][-region]-u-ca-*
325 |
326 |
327 |
328 | - reference value
329 | - Refer to for other values:http://www.lingoes.cn/zh/translator/langcode.htm
330 | ```javascript
331 | "zh-Hans" //Simplified Chinese
332 | "zh-TW" //Traditional Chinese
333 | "ru-RU" //Russian
334 | "en-GB" //britain
335 | "it-IT" //Italy
336 | "fr-FR" //French
337 | "pt-PT" //Portugal
338 | "de-DE" //German
339 | "en-US" //usa
340 | "en-AU" //澳大利亚
341 | "en-CA" //Australia
342 | "ko-KR" //the republic of korea
343 | "ar-EG" //arab
344 | "ja-JP" //Japan
345 | "nl-NL" //Netherlands
346 | "es-ES" //Spain
347 | "hi-IN" //Hindi
348 | "eo" //Esperanto
349 | ```
350 |
351 |
352 |
353 | # `options` parameter
354 | * type:`object`
355 |
356 | * The built-in default parameter is
357 | ```javascript
358 | const DEFAULT_OPTIONS = {
359 | year: "numeric",
360 | month: "numeric",
361 | day: "numeric",
362 | hour: "numeric",
363 | minute: "numeric",
364 | second: "numeric",
365 | weekday: "short",
366 | hour12:false,
367 | timeZoneName :"short",
368 | }
369 | ```
370 |
371 | ### Configurable parameters. If they have the same properties as the default parameters, the passed in parameters will prevail
372 | ```javascript
373 | let options = {
374 | era: "long", //Show ad:"narrow", "short", "long"
375 | year: "numeric", //"numeric", "2-digit"
376 | month: "numeric", //"numeric", "2-digit", "narrow", "short", "long"
377 | day: "numeric", //"numeric", "2-digit"
378 | hour: "numeric", //"numeric", "2-digit"
379 | minute: "numeric",//"numeric", "2-digit"
380 | second: "numeric",//"numeric", "2-digit"
381 | weekday: "long", //"narrow", "short", "long"
382 | hour12:false, // true | false
383 | timeZoneName :"short", //"short", "long"
384 | timeZone:"", // Specify the time zone for the city
385 | }
386 | ```
387 |
388 | ## Reference value of:`options.timeZone`
389 | - For other values, please refer to:https://www.iana.org/time-zones
390 | ```javascript
391 | timeZone:'UTC' // UTC time
392 | timeZone:'Europe/Berlin' // GMT+1; German time zone
393 | timeZone:'Australia/Sydney' // GMT+11; Australian time zone
394 | timeZone:'Europe/Moscow' // GMT+3; Russian time zone
395 | timeZone:'Europe/Paris' // GMT+1; French time zone
396 | timeZone:'Europe/London' // GMT; UK time zone
397 | timeZone:'Asia/Tokyo' // JST; Tokyo time zone
398 | timeZone:'Asia/Shanghai' // GMT+8; Beijing time zone
399 | timeZone:'America/Denver' // MST; Us mountain time zone
400 | timeZone:'America/Los_Angeles' // PTZ; Pacific time zone
401 | timeZone:'America/New_York' // EST; Eastern time zone
402 | timeZone:'America/Chicago' // CST; Central time zone
403 | ```
404 |
405 |
406 |
407 |
408 |
409 | # Version update history
410 |
411 | ## v2.2.2
412 | Update document parameter description
413 |
414 | ## v2.2.1
415 | Update document example reference
416 |
417 | ## v2.2.0
418 | The corresponding time can be specified for all API additions
419 |
420 | ## v2.1.2
421 | Verification of development and production environment
422 |
423 | ## v2.1.1
424 | add tsconfig.json
425 |
426 | ## v2.1.0
427 | Change the JS of the library to ts
--------------------------------------------------------------------------------
/data.ts:
--------------------------------------------------------------------------------
1 |
2 |
3 | export const week:string[] = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
4 | export const DEFAULT_OPTIONS:DEFAULT_OPTIONS = {
5 | year: "numeric",
6 | month: "numeric",
7 | day: "numeric",
8 | hour: "numeric",
9 | minute: "numeric",
10 | second: "numeric",
11 | weekday: "short",
12 | hour12:false,
13 | timeZoneName :"short",
14 | }
15 | export const getSymbol = (symbol?:string):string => {
16 | return symbol?symbol:'/'
17 | }
18 |
19 | export interface DEFAULT_OPTIONS {
20 | year?: "numeric" | "2-digit",
21 | month?: "long" | "short" | "narrow" | "numeric" | "2-digit",
22 | day?: "numeric" | "2-digit",
23 | hour?: "numeric" | "2-digit",
24 | minute?: "numeric" | "2-digit",
25 | second?: "numeric" | "2-digit",
26 | weekday?: "long" | "short" | "narrow",
27 | hour12?:boolean,
28 | timeZoneName?:"long" | "short",
29 | timeZone ?:string,
30 | }
--------------------------------------------------------------------------------
/index.ts:
--------------------------------------------------------------------------------
1 | (function toLocaleStringSupportsLocales() {
2 | try {
3 | new Date().toLocaleString("i");
4 | } catch (e) {
5 | return
6 | }
7 | console.error('该浏览器环境可能不支持 @time-zone/date 库');
8 | })()
9 | export * from './operation'
10 | export * from './time-zone'
--------------------------------------------------------------------------------
/operation.ts:
--------------------------------------------------------------------------------
1 | type DatePara = string | number | Date | null | undefined
2 | type _Date = string | number | Date
3 | import {getESTDate,getWorldTimeZone} from './time-zone'
4 | import { week,DEFAULT_OPTIONS,getSymbol } from './data'
5 |
6 | /** @以下是获取UTC0时区的数据 */
7 | export const UTC_getYear = (datePara?:_Date)=> new Date(datePara as _Date).getUTCFullYear()
8 | export const UTC_getMonth = (datePara?:_Date)=> new Date(datePara as _Date).getUTCMonth()
9 | export const UTC_getDate = (datePara?:_Date)=> new Date(datePara as _Date).getUTCDate()
10 | export const UTC_getHours = (datePara?:_Date)=> new Date(datePara as _Date).getUTCHours()
11 | export const UTC_getMinutes = (datePara?:_Date)=> new Date(datePara as _Date).getUTCMinutes()
12 | export const UTC_getSeconds = (datePara?:_Date)=> new Date(datePara as _Date).getUTCSeconds()
13 |
14 |
15 | /** @以下是获取指定时区的指定数据 */
16 | // 不传则默认代码运行环境时的当前时区
17 | export const get_mmddyyyy = (region:string,symbol?:string,datePara?:DatePara)=> {
18 | datePara = datePara ? datePara:new Date();
19 | let sym = getSymbol(symbol)
20 | let date = getWorldTimeZone(datePara,'en-GB',{timeZone:region})
21 | let dmy = date.split(' ')[1].replace(',','').split('/')
22 | let y = dmy[2]
23 | let m = dmy[1]
24 | let d = dmy[0]
25 | return `${m+sym}${d+sym}${y}` // 月 日 年
26 | }
27 | export const get_ddmmyyyy = (region:string,symbol?:string,datePara?:DatePara)=> {
28 | datePara = datePara ? datePara:new Date();
29 | let sym = getSymbol(symbol)
30 | let date = getWorldTimeZone(datePara,'en-GB',{timeZone:region})
31 | let dmy = date.split(' ')[1].replace(',','').split('/')
32 | let y = dmy[2]
33 | let m = dmy[1]
34 | let d = dmy[0]
35 | return `${d+sym}${m+sym}${y}` // 日 月 年
36 | }
37 | export const get_yyyymmdd = (region:string,symbol?:string,datePara?:DatePara)=> {
38 | datePara = datePara ? datePara:new Date();
39 | let sym = getSymbol(symbol)
40 | let date = getWorldTimeZone(datePara,'en-GB',{timeZone:region})
41 | let dmy = date.split(' ')[1].replace(',','').split('/')
42 | let y = dmy[2]
43 | let m = dmy[1]
44 | let d = dmy[0]
45 | return `${y+sym}${m+sym}${d}` // 年 月 日
46 | }
47 | export const custom_yyyymmdd_sort = (region:string,ymdsort?:string,symbol?:string,datePara?:DatePara)=> {
48 | datePara = datePara ? datePara:new Date();
49 | let sym = getSymbol(symbol)
50 | let date = getWorldTimeZone(datePara,'en-GB',{timeZone:region})
51 | let dmy = date.split(' ')[1].replace(',','').split('/')
52 | let y = dmy[2]
53 | let m = dmy[1]
54 | let d = dmy[0]
55 | let returnData = ''
56 | if (ymdsort) {
57 | let sort:string[] = ymdsort.split('')
58 | sort.forEach((e,i) => {
59 | switch (e) {
60 | case 'y':
61 | returnData+=(y + (i==sort.length-1?'':sym))
62 | break;
63 | case 'm':
64 | returnData+=(m + (i==sort.length-1?'':sym))
65 | break;
66 | case 'd':
67 | returnData+=(d + (i==sort.length-1?'':sym))
68 | break;
69 | }
70 | });
71 | }else{
72 | returnData = `${y+sym}${m+sym}${d}`
73 | }
74 | return returnData // 自定义顺序
75 | }
76 | export const custom_ymd_sort = (region:string,ymdsort?:string,symbol?:string,datePara?:DatePara)=> {
77 | datePara = datePara ? datePara:new Date();
78 | let sym = getSymbol(symbol)
79 | let date = getWorldTimeZone(datePara,'en-US',{timeZone:region})
80 | let dmy = date.split(' ')[1].replace(',','').split('/')
81 | let y = dmy[2]
82 | let m = dmy[0]
83 | let d = dmy[1]
84 | let returnData = ''
85 | if (ymdsort) {
86 | let sort = ymdsort.split('')
87 | sort.forEach((e,i) => {
88 | switch (e) {
89 | case 'y':
90 | returnData+=(y + (i==sort.length-1?'':sym))
91 | break;
92 | case 'm':
93 | returnData+=(m + (i==sort.length-1?'':sym))
94 | break;
95 | case 'd':
96 | returnData+=(d + (i==sort.length-1?'':sym))
97 | break;
98 | }
99 | });
100 | }else{
101 | returnData = `${y+sym}${m+sym}${d}`
102 | }
103 | return returnData // 自定义顺序
104 | }
105 |
106 | export const get_hms = (region:string,datePara?:DatePara):string => {
107 | datePara = datePara ? datePara:new Date();
108 | let date = getWorldTimeZone(datePara,'en-US',{timeZone:region})
109 | return date.split(' ')[2] // 时 分 秒
110 | }
111 |
112 | export const getYear = (region:string,datePara?:DatePara)=>{
113 | datePara = datePara ? datePara:new Date();
114 | return Number(get_mmddyyyy(region,'/',datePara).split('/')[2])
115 | }
116 | export const getMonth = (region:string,datePara?:DatePara)=>{
117 | datePara = datePara ? datePara:new Date();
118 | return Number(get_mmddyyyy(region,'/',datePara).split('/')[0]) - 1
119 | }
120 | export const getDate = (region:string,datePara?:DatePara)=>{
121 | datePara = datePara ? datePara:new Date();
122 | return Number(get_mmddyyyy(region,'/',datePara).split('/')[1])
123 | }
124 | // 默认24小时制
125 | export const getHours = (region:string,datePara?:DatePara)=> {
126 | datePara = datePara ? datePara:new Date();
127 | let h = Number(get_hms(region,datePara).split(':')[0])
128 | if (h === 24) {
129 | h = 0
130 | }
131 | return Number(h)
132 | }
133 | export const getMinutes = (region:string,datePara?:DatePara)=> {
134 | datePara = datePara ? datePara:new Date();
135 | let m = get_hms(region,datePara).split(':')[1]
136 | return Number(m)
137 | }
138 | export const getSeconds = (region:string,datePara?:DatePara)=> {
139 | datePara = datePara ? datePara:new Date();
140 | let s = get_hms(region,datePara).split(':')[2]
141 | return Number(s)
142 | }
143 | export const getDay = (region:string,datePara?:DatePara)=> {
144 | datePara = datePara ? datePara:new Date();
145 | DEFAULT_OPTIONS.timeZone = region
146 | let date = new Date(datePara).toLocaleString('en-US',DEFAULT_OPTIONS)
147 | let day = date.split(' ')[0].replace(',','')
148 | let DAY = week.findIndex((e:string)=>{
149 | return e == day
150 | })
151 | return DAY
152 | }
153 |
154 |
155 |
156 |
157 | /** @以下是EST时间 */
158 | export const getESTmmddyyyy = (datePara?:_Date)=> {
159 | return getESTDate(datePara).split(' ')[1].replace(',','') // 月 日 年
160 | }
161 | export const getESThms = (datePara?:_Date)=> {
162 | return getESTDate(datePara).split(' ')[2] // 时 分 秒
163 | }
164 | export const EST_getYear = (datePara?:_Date)=> {
165 | return Number(getESTmmddyyyy(datePara).split('/')[2])
166 | }
167 | export const EST_getMonth = (datePara?:_Date)=> {
168 | return Number(getESTmmddyyyy(datePara).split('/')[0])-1
169 | }
170 | export const EST_getDate = (datePara?:_Date)=> {
171 | return Number(getESTmmddyyyy(datePara).split('/')[1])
172 | }
173 | export const EST_getHours = (is0?:number,datePara?:_Date)=> {
174 | let data = Number(getESThms(datePara).split(':')[0])
175 | if (is0 === 0 && Number(data) === 24) {
176 | data = 0
177 | }
178 | return Number(data)
179 | }
180 | export const EST_getMinutes = (datePara?:_Date)=> {
181 | return Number(getESThms(datePara).split(':')[1])
182 | }
183 | export const EST_getSeconds = (datePara?:_Date)=> {
184 | return Number(getESThms(datePara).split(':')[2])
185 | }
186 | export const EST_getDay = (datePara?:_Date)=> {
187 | let usDay = getESTDate(datePara).split(' ')[0].replace(',','')
188 | let DAY = week.findIndex((e:string)=>{
189 | return e == usDay
190 | })
191 | return DAY
192 | }
193 | /** @以上是EST时间 */
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@time-zone/date",
3 | "version": "2.2.2",
4 | "description": "让代码运行在不同时区(国家)环境下,都能准确获得指定时区(国家)的时间!Let the code run in different time zones (countries) environment, can accurately obtain the time of the specified time zone (country)!",
5 | "main": "index.ts",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/Oumae-Kumiko/time-zone-date.git"
12 | },
13 | "keywords": [
14 | "timezone",
15 | "date",
16 | "moment",
17 | "custom format",
18 | "locale",
19 | "operation",
20 | "typescript",
21 | "i18n",
22 | "time zone"
23 | ],
24 | "author": "李嘉辉,645770665@qq.com",
25 | "license": "ISC",
26 | "bugs": {
27 | "url": "https://github.com/Oumae-Kumiko/time-zone-date/issues"
28 | },
29 | "homepage": "https://github.com/Oumae-Kumiko/time-zone-date#readme"
30 | }
31 |
--------------------------------------------------------------------------------
/time-zone.ts:
--------------------------------------------------------------------------------
1 | type DatePara = string | number | Date | null | undefined
2 |
3 | import {
4 | UTC_getYear,UTC_getMonth,UTC_getDate,UTC_getHours,UTC_getMinutes,UTC_getSeconds,
5 | EST_getYear,EST_getMonth,EST_getDate,EST_getHours,EST_getMinutes,EST_getSeconds
6 | } from './operation'
7 | import { DEFAULT_OPTIONS } from './data'
8 | let isObject = (type:any)=>{
9 | return Object.prototype.toString.call(type) === '[object Object]'
10 | }
11 | export const getUTCDate = (datePara?:DatePara)=> {
12 | datePara = datePara ? datePara:new Date();
13 | return new Date(datePara).toUTCString()
14 | }
15 | export const getESTDate = (datePara?:DatePara)=> {
16 | datePara = datePara ? datePara:new Date();
17 | DEFAULT_OPTIONS.timeZone = "America/New_York";
18 | return new Date(datePara).toLocaleString("en-US",DEFAULT_OPTIONS)
19 | }
20 | export const getWorldTimeZone = (datePara?:DatePara,locales?:string,options?:DEFAULT_OPTIONS)=> {
21 | if (!isObject(options)&&options) {
22 | console.error('getWorldTimeZone() 请传入正确 options 选项');
23 | }
24 | let data = {
25 | ...DEFAULT_OPTIONS,
26 | ...options
27 | }
28 | datePara = datePara ? datePara:new Date();
29 | return new Date(datePara).toLocaleString(locales,data)
30 | }
31 |
32 | export const isUS_Dst = ()=> {
33 | // 美国现在是否夏令时 GMT-4
34 | let UTCyear = UTC_getYear()
35 | let UTCmonth = UTC_getMonth()
36 | let UTCdate = UTC_getDate()
37 | let UTChours = UTC_getHours()
38 | let UTCminutes = UTC_getMinutes()
39 | let UTCseconds = UTC_getSeconds()
40 | let us = +new Date(
41 | EST_getYear(),
42 | EST_getMonth(),
43 | EST_getDate(),
44 | EST_getHours(0),
45 | EST_getMinutes(),
46 | EST_getSeconds(),
47 | )
48 | let utc = +new Date(UTCyear,UTCmonth,UTCdate,UTChours,UTCminutes,UTCseconds)
49 | let timeDiff = (us - utc)/1000/60/60
50 | if (timeDiff == -5) {
51 | return false
52 | }else{
53 | return true
54 | }
55 | }
56 | export const isUS_Wt = ()=> {
57 | // 美国现在是否冬令时 GMT-5
58 | return !isUS_Dst()
59 | }
60 |
61 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "es2015",
4 | "declaration": true,
5 | "strict": true,
6 | "moduleResolution": "node",
7 | "sourceMap": false,
8 | "lib": ["ES2015","ESNext","DOM"],
9 | },
10 | }
--------------------------------------------------------------------------------