react-util-tools 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +254 -0
- package/dist/index.cjs +943 -0
- package/dist/index.d.cts +154 -0
- package/dist/index.d.ts +154 -0
- package/dist/index.js +852 -0
- package/package.json +50 -0
- package/src/address/README.md +196 -0
- package/src/address/index.ts +41 -0
- package/src/date/README.md +539 -0
- package/src/date/index.ts +330 -0
- package/src/date/utc/README.md +779 -0
- package/src/date/utc/index.ts +374 -0
- package/src/decimal/README.md +425 -0
- package/src/decimal/index.ts +9 -0
- package/src/decimal/utils/README.md +474 -0
- package/src/decimal/utils/index.ts +244 -0
- package/src/device/README.md +441 -0
- package/src/device/index.ts +214 -0
- package/src/format/README.md +335 -0
- package/src/format/index.ts +189 -0
- package/src/index.ts +107 -0
- package/src/throttle/README.md +152 -0
- package/src/throttle/index.ts +83 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,779 @@
|
|
|
1
|
+
# UTC 时区工具
|
|
2
|
+
|
|
3
|
+
专门处理 UTC+0 时区的日期时间工具函数集合。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install your-package-name date-fns date-fns-tz
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用方法
|
|
12
|
+
|
|
13
|
+
### 导入
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import {
|
|
17
|
+
toUTC,
|
|
18
|
+
fromUTC,
|
|
19
|
+
formatUTC,
|
|
20
|
+
formatUTCDateOnly,
|
|
21
|
+
formatUTCTimeOnly,
|
|
22
|
+
toISOString,
|
|
23
|
+
getUTCNow,
|
|
24
|
+
getUTCTimestamp,
|
|
25
|
+
getUTCTimestampInSeconds,
|
|
26
|
+
getUTCStartOfDay,
|
|
27
|
+
getUTCEndOfDay,
|
|
28
|
+
getUTCStartOfMonth,
|
|
29
|
+
getUTCEndOfMonth,
|
|
30
|
+
addDaysUTC,
|
|
31
|
+
subDaysUTC,
|
|
32
|
+
addMonthsUTC,
|
|
33
|
+
subMonthsUTC,
|
|
34
|
+
getUTCDaysDiff,
|
|
35
|
+
getUTCHoursDiff,
|
|
36
|
+
getUTCMinutesDiff,
|
|
37
|
+
getTimezoneOffset,
|
|
38
|
+
getTimezoneOffsetHours
|
|
39
|
+
} from 'your-package-name/date/utc'
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
### toUTC - 本地时间转 UTC
|
|
45
|
+
|
|
46
|
+
将本地时间转换为 UTC 时间。
|
|
47
|
+
|
|
48
|
+
**参数:**
|
|
49
|
+
- `date`: 本地日期对象、时间戳或日期字符串
|
|
50
|
+
|
|
51
|
+
**返回:** UTC Date 对象
|
|
52
|
+
|
|
53
|
+
**示例:**
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
// 假设当前在 UTC+8 时区(中国)
|
|
57
|
+
const localDate = new Date('2024-11-10 15:30:00') // 本地时间
|
|
58
|
+
|
|
59
|
+
const utcDate = toUTC(localDate)
|
|
60
|
+
console.log(utcDate)
|
|
61
|
+
// 2024-11-10 07:30:00 UTC(减去8小时)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### fromUTC - UTC 转本地时间
|
|
65
|
+
|
|
66
|
+
将 UTC 时间转换为本地时间。
|
|
67
|
+
|
|
68
|
+
**参数:**
|
|
69
|
+
- `utcDate`: UTC 日期对象、时间戳或日期字符串
|
|
70
|
+
|
|
71
|
+
**返回:** 本地 Date 对象
|
|
72
|
+
|
|
73
|
+
**示例:**
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
// 假设当前在 UTC+8 时区(中国)
|
|
77
|
+
const utcDate = new Date('2024-11-10T07:30:00.000Z') // UTC 时间
|
|
78
|
+
|
|
79
|
+
const localDate = fromUTC(utcDate)
|
|
80
|
+
console.log(localDate)
|
|
81
|
+
// 2024-11-10 15:30:00(加上8小时)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### formatUTC - 格式化为 UTC 时间
|
|
85
|
+
|
|
86
|
+
将日期格式化为 UTC 时区的字符串。
|
|
87
|
+
|
|
88
|
+
**参数:**
|
|
89
|
+
- `date`: 日期对象、时间戳或日期字符串
|
|
90
|
+
- `formatStr`: 格式化字符串,默认 `'yyyy-MM-dd HH:mm:ss'`
|
|
91
|
+
|
|
92
|
+
**返回:** UTC 时间字符串
|
|
93
|
+
|
|
94
|
+
**示例:**
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const date = new Date('2024-11-10T15:30:00+08:00') // 中国时间
|
|
98
|
+
|
|
99
|
+
formatUTC(date)
|
|
100
|
+
// "2024-11-10 07:30:00" (UTC 时间)
|
|
101
|
+
|
|
102
|
+
formatUTC(date, 'yyyy-MM-dd HH:mm')
|
|
103
|
+
// "2024-11-10 07:30"
|
|
104
|
+
|
|
105
|
+
formatUTC(date, 'yyyy年MM月dd日 HH:mm:ss')
|
|
106
|
+
// "2024年11月10日 07:30:00"
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### formatUTCDateOnly - 格式化为 UTC 日期
|
|
110
|
+
|
|
111
|
+
只格式化 UTC 日期部分,不含时间。
|
|
112
|
+
|
|
113
|
+
**示例:**
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
const date = new Date('2024-11-10T15:30:00+08:00')
|
|
117
|
+
|
|
118
|
+
formatUTCDateOnly(date)
|
|
119
|
+
// "2024-11-10"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### formatUTCTimeOnly - 格式化为 UTC 时间
|
|
123
|
+
|
|
124
|
+
只格式化 UTC 时间部分,不含日期。
|
|
125
|
+
|
|
126
|
+
**示例:**
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
const date = new Date('2024-11-10T15:30:00+08:00')
|
|
130
|
+
|
|
131
|
+
formatUTCTimeOnly(date)
|
|
132
|
+
// "07:30:00"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### toISOString - 格式化为 ISO 8601 UTC 字符串
|
|
136
|
+
|
|
137
|
+
将日期格式化为标准的 ISO 8601 格式(UTC 时间)。
|
|
138
|
+
|
|
139
|
+
**示例:**
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
const date = new Date('2024-11-10 15:30:00')
|
|
143
|
+
|
|
144
|
+
toISOString(date)
|
|
145
|
+
// "2024-11-10T07:30:00.000Z"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### getUTCNow - 获取 UTC 当前时间
|
|
149
|
+
|
|
150
|
+
**示例:**
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
const now = getUTCNow()
|
|
154
|
+
console.log(now)
|
|
155
|
+
// Date 对象(UTC 时间)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### getUTCTimestamp - 获取 UTC 当前时间戳(毫秒)
|
|
159
|
+
|
|
160
|
+
**示例:**
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
getUTCTimestamp()
|
|
164
|
+
// 1699612800000
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### getUTCTimestampInSeconds - 获取 UTC 当前时间戳(秒)
|
|
168
|
+
|
|
169
|
+
**示例:**
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
getUTCTimestampInSeconds()
|
|
173
|
+
// 1699612800
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### getUTCStartOfDay - 获取 UTC 一天的开始时间
|
|
177
|
+
|
|
178
|
+
获取指定日期在 UTC 时区的当天 00:00:00。
|
|
179
|
+
|
|
180
|
+
**示例:**
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
const date = new Date('2024-11-10T15:30:00+08:00')
|
|
184
|
+
|
|
185
|
+
getUTCStartOfDay(date)
|
|
186
|
+
// 2024-11-10 00:00:00 UTC
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### getUTCEndOfDay - 获取 UTC 一天的结束时间
|
|
190
|
+
|
|
191
|
+
获取指定日期在 UTC 时区的当天 23:59:59。
|
|
192
|
+
|
|
193
|
+
**示例:**
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const date = new Date('2024-11-10T15:30:00+08:00')
|
|
197
|
+
|
|
198
|
+
getUTCEndOfDay(date)
|
|
199
|
+
// 2024-11-10 23:59:59 UTC
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### getUTCStartOfMonth - 获取 UTC 一个月的开始时间
|
|
203
|
+
|
|
204
|
+
**示例:**
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
const date = new Date('2024-11-10')
|
|
208
|
+
|
|
209
|
+
getUTCStartOfMonth(date)
|
|
210
|
+
// 2024-11-01 00:00:00 UTC
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### getUTCEndOfMonth - 获取 UTC 一个月的结束时间
|
|
214
|
+
|
|
215
|
+
**示例:**
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
const date = new Date('2024-11-10')
|
|
219
|
+
|
|
220
|
+
getUTCEndOfMonth(date)
|
|
221
|
+
// 2024-11-30 23:59:59 UTC
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### addDaysUTC - UTC 时间增加天数
|
|
225
|
+
|
|
226
|
+
**示例:**
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
const date = new Date('2024-11-10T00:00:00Z')
|
|
230
|
+
|
|
231
|
+
addDaysUTC(date, 7)
|
|
232
|
+
// 2024-11-17 00:00:00 UTC
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### subDaysUTC - UTC 时间减少天数
|
|
236
|
+
|
|
237
|
+
**示例:**
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
const date = new Date('2024-11-10T00:00:00Z')
|
|
241
|
+
|
|
242
|
+
subDaysUTC(date, 7)
|
|
243
|
+
// 2024-11-03 00:00:00 UTC
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### addMonthsUTC - UTC 时间增加月数
|
|
247
|
+
|
|
248
|
+
**示例:**
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
const date = new Date('2024-11-10T00:00:00Z')
|
|
252
|
+
|
|
253
|
+
addMonthsUTC(date, 2)
|
|
254
|
+
// 2025-01-10 00:00:00 UTC
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### subMonthsUTC - UTC 时间减少月数
|
|
258
|
+
|
|
259
|
+
**示例:**
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
const date = new Date('2024-11-10T00:00:00Z')
|
|
263
|
+
|
|
264
|
+
subMonthsUTC(date, 2)
|
|
265
|
+
// 2024-09-10 00:00:00 UTC
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### getUTCDaysDiff - 计算 UTC 时间天数差
|
|
269
|
+
|
|
270
|
+
**示例:**
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
const date1 = new Date('2024-11-10T00:00:00Z')
|
|
274
|
+
const date2 = new Date('2024-11-15T00:00:00Z')
|
|
275
|
+
|
|
276
|
+
getUTCDaysDiff(date2, date1)
|
|
277
|
+
// 5
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### getUTCHoursDiff - 计算 UTC 时间小时差
|
|
281
|
+
|
|
282
|
+
**示例:**
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
const date1 = new Date('2024-11-10T10:00:00Z')
|
|
286
|
+
const date2 = new Date('2024-11-10T15:30:00Z')
|
|
287
|
+
|
|
288
|
+
getUTCHoursDiff(date2, date1)
|
|
289
|
+
// 5
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### getUTCMinutesDiff - 计算 UTC 时间分钟差
|
|
293
|
+
|
|
294
|
+
**示例:**
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
const date1 = new Date('2024-11-10T10:00:00Z')
|
|
298
|
+
const date2 = new Date('2024-11-10T10:30:00Z')
|
|
299
|
+
|
|
300
|
+
getUTCMinutesDiff(date2, date1)
|
|
301
|
+
// 30
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### getTimezoneOffset - 获取时区偏移量(分钟)
|
|
305
|
+
|
|
306
|
+
获取当前时区相对于 UTC 的偏移量(分钟)。
|
|
307
|
+
|
|
308
|
+
**返回:** 时区偏移量,例如:中国为 -480(UTC+8)
|
|
309
|
+
|
|
310
|
+
**示例:**
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
// 在中国(UTC+8)
|
|
314
|
+
getTimezoneOffset()
|
|
315
|
+
// -480
|
|
316
|
+
|
|
317
|
+
// 在英国(UTC+0)
|
|
318
|
+
getTimezoneOffset()
|
|
319
|
+
// 0
|
|
320
|
+
|
|
321
|
+
// 在美国东部(UTC-5)
|
|
322
|
+
getTimezoneOffset()
|
|
323
|
+
// 300
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### getTimezoneOffsetHours - 获取时区偏移量(小时)
|
|
327
|
+
|
|
328
|
+
获取当前时区相对于 UTC 的偏移量(小时)。
|
|
329
|
+
|
|
330
|
+
**返回:** 时区偏移量,例如:中国为 8(UTC+8)
|
|
331
|
+
|
|
332
|
+
**示例:**
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
// 在中国(UTC+8)
|
|
336
|
+
getTimezoneOffsetHours()
|
|
337
|
+
// 8
|
|
338
|
+
|
|
339
|
+
// 在英国(UTC+0)
|
|
340
|
+
getTimezoneOffsetHours()
|
|
341
|
+
// 0
|
|
342
|
+
|
|
343
|
+
// 在美国东部(UTC-5)
|
|
344
|
+
getTimezoneOffsetHours()
|
|
345
|
+
// -5
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## 实际应用场景
|
|
349
|
+
|
|
350
|
+
### 后端 API 交互
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
import { toISOString, fromUTC } from 'your-package-name/date/utc'
|
|
354
|
+
|
|
355
|
+
// 发送给后端(转为 UTC ISO 字符串)
|
|
356
|
+
const localDate = new Date('2024-11-10 15:30:00')
|
|
357
|
+
const apiData = {
|
|
358
|
+
startDate: toISOString(localDate)
|
|
359
|
+
// "2024-11-10T07:30:00.000Z"
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// 从后端接收(UTC 转本地时间)
|
|
363
|
+
const response = {
|
|
364
|
+
createdAt: '2024-11-10T07:30:00.000Z'
|
|
365
|
+
}
|
|
366
|
+
const localCreatedAt = fromUTC(response.createdAt)
|
|
367
|
+
// 2024-11-10 15:30:00(本地时间)
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### 跨时区数据展示
|
|
371
|
+
|
|
372
|
+
```typescript
|
|
373
|
+
import { formatUTC, getTimezoneOffsetHours } from 'your-package-name/date/utc'
|
|
374
|
+
|
|
375
|
+
const serverTime = '2024-11-10T10:00:00.000Z' // 服务器 UTC 时间
|
|
376
|
+
|
|
377
|
+
// 显示 UTC 时间
|
|
378
|
+
console.log(`UTC 时间: ${formatUTC(serverTime)}`)
|
|
379
|
+
// "UTC 时间: 2024-11-10 10:00:00"
|
|
380
|
+
|
|
381
|
+
// 显示当前时区
|
|
382
|
+
const offset = getTimezoneOffsetHours()
|
|
383
|
+
console.log(`当前时区: UTC${offset >= 0 ? '+' : ''}${offset}`)
|
|
384
|
+
// "当前时区: UTC+8"
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### 日志记录
|
|
388
|
+
|
|
389
|
+
```typescript
|
|
390
|
+
import { formatUTC, getUTCTimestamp } from 'your-package-name/date/utc'
|
|
391
|
+
|
|
392
|
+
// 记录日志时使用 UTC 时间
|
|
393
|
+
const log = {
|
|
394
|
+
message: '用户登录',
|
|
395
|
+
timestamp: getUTCTimestamp(),
|
|
396
|
+
time: formatUTC(new Date())
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
console.log(log)
|
|
400
|
+
// {
|
|
401
|
+
// message: '用户登录',
|
|
402
|
+
// timestamp: 1699612800000,
|
|
403
|
+
// time: '2024-11-10 07:30:00'
|
|
404
|
+
// }
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### 定时任务
|
|
408
|
+
|
|
409
|
+
```typescript
|
|
410
|
+
import { getUTCStartOfDay, formatUTC } from 'your-package-name/date/utc'
|
|
411
|
+
|
|
412
|
+
// 计算下一个 UTC 0点的时间
|
|
413
|
+
const now = new Date()
|
|
414
|
+
const nextUTCMidnight = getUTCStartOfDay(addDaysUTC(now, 1))
|
|
415
|
+
|
|
416
|
+
console.log(`下次执行时间(UTC): ${formatUTC(nextUTCMidnight)}`)
|
|
417
|
+
// "下次执行时间(UTC): 2024-11-11 00:00:00"
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### 数据统计(UTC 时间范围)
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
import { getUTCStartOfMonth, getUTCEndOfMonth, formatUTC } from 'your-package-name/date/utc'
|
|
424
|
+
|
|
425
|
+
const today = new Date()
|
|
426
|
+
|
|
427
|
+
// 获取本月的 UTC 时间范围
|
|
428
|
+
const monthStart = getUTCStartOfMonth(today)
|
|
429
|
+
const monthEnd = getUTCEndOfMonth(today)
|
|
430
|
+
|
|
431
|
+
fetchData({
|
|
432
|
+
startDate: formatUTC(monthStart, 'yyyy-MM-dd'),
|
|
433
|
+
endDate: formatUTC(monthEnd, 'yyyy-MM-dd')
|
|
434
|
+
})
|
|
435
|
+
// {
|
|
436
|
+
// startDate: "2024-11-01",
|
|
437
|
+
// endDate: "2024-11-30"
|
|
438
|
+
// }
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
### 时区转换显示
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
import { formatUTC, fromUTC, getTimezoneOffsetHours } from 'your-package-name/date/utc'
|
|
445
|
+
|
|
446
|
+
const utcTime = '2024-11-10T10:00:00.000Z'
|
|
447
|
+
|
|
448
|
+
// 显示多个时区的时间
|
|
449
|
+
console.log(`UTC 时间: ${formatUTC(utcTime)}`)
|
|
450
|
+
// "UTC 时间: 2024-11-10 10:00:00"
|
|
451
|
+
|
|
452
|
+
console.log(`本地时间: ${formatDate(fromUTC(utcTime))}`)
|
|
453
|
+
// "本地时间: 2024-11-10 18:00:00" (假设在 UTC+8)
|
|
454
|
+
|
|
455
|
+
const offset = getTimezoneOffsetHours()
|
|
456
|
+
console.log(`时区偏移: UTC${offset >= 0 ? '+' : ''}${offset}`)
|
|
457
|
+
// "时区偏移: UTC+8"
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
## TypeScript 支持
|
|
461
|
+
|
|
462
|
+
完整的 TypeScript 类型支持。
|
|
463
|
+
|
|
464
|
+
```typescript
|
|
465
|
+
const utcDate: Date = toUTC(new Date())
|
|
466
|
+
const localDate: Date = fromUTC(new Date())
|
|
467
|
+
const formatted: string = formatUTC(new Date())
|
|
468
|
+
const timestamp: number = getUTCTimestamp()
|
|
469
|
+
const offset: number = getTimezoneOffset()
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
## 注意事项
|
|
473
|
+
|
|
474
|
+
- 所有 UTC 函数都基于 UTC+0 时区进行计算
|
|
475
|
+
- `toUTC` 和 `fromUTC` 用于本地时间和 UTC 时间的相互转换
|
|
476
|
+
- 时间戳(`getUTCTimestamp`)本身就是 UTC 时间,与时区无关
|
|
477
|
+
- 使用 `toISOString` 可以获得标准的 ISO 8601 格式(后端 API 常用)
|
|
478
|
+
- 时区偏移量:正数表示东时区(如 UTC+8),负数表示西时区(如 UTC-5)
|
|
479
|
+
- 建议在与后端交互时统一使用 UTC 时间,避免时区混淆
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
### getUTCYearStartTimestamp - 获取年份第一天时间戳
|
|
483
|
+
|
|
484
|
+
获取指定年份 UTC 第一天 00:00:00 的时间戳(毫秒)。
|
|
485
|
+
|
|
486
|
+
**参数:**
|
|
487
|
+
- `year`: 年份,默认当前年份
|
|
488
|
+
|
|
489
|
+
**返回:** 时间戳(毫秒)
|
|
490
|
+
|
|
491
|
+
**示例:**
|
|
492
|
+
|
|
493
|
+
```typescript
|
|
494
|
+
// 获取 2024 年第一天的时间戳
|
|
495
|
+
getUTCYearStartTimestamp(2024)
|
|
496
|
+
// 1704067200000 (2024-01-01 00:00:00 UTC)
|
|
497
|
+
|
|
498
|
+
// 获取当前年份第一天的时间戳
|
|
499
|
+
getUTCYearStartTimestamp()
|
|
500
|
+
// 当前年份 1 月 1 日 00:00:00 UTC 的时间戳
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
### getUTCYearEndTimestamp - 获取年份最后一天时间戳
|
|
504
|
+
|
|
505
|
+
获取指定年份 UTC 最后一天 23:59:59 的时间戳(毫秒)。
|
|
506
|
+
|
|
507
|
+
**参数:**
|
|
508
|
+
- `year`: 年份,默认当前年份
|
|
509
|
+
|
|
510
|
+
**返回:** 时间戳(毫秒)
|
|
511
|
+
|
|
512
|
+
**示例:**
|
|
513
|
+
|
|
514
|
+
```typescript
|
|
515
|
+
// 获取 2024 年最后一天的时间戳
|
|
516
|
+
getUTCYearEndTimestamp(2024)
|
|
517
|
+
// 1735689599999 (2024-12-31 23:59:59.999 UTC)
|
|
518
|
+
|
|
519
|
+
// 获取当前年份最后一天的时间戳
|
|
520
|
+
getUTCYearEndTimestamp()
|
|
521
|
+
// 当前年份 12 月 31 日 23:59:59 UTC 的时间戳
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
### getUTCYearStart - 获取年份第一天 Date 对象
|
|
525
|
+
|
|
526
|
+
获取指定年份 UTC 第一天 00:00:00 的 Date 对象。
|
|
527
|
+
|
|
528
|
+
**示例:**
|
|
529
|
+
|
|
530
|
+
```typescript
|
|
531
|
+
getUTCYearStart(2024)
|
|
532
|
+
// Date 对象: 2024-01-01 00:00:00 UTC
|
|
533
|
+
|
|
534
|
+
getUTCYearStart()
|
|
535
|
+
// 当前年份第一天
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
### getUTCYearEnd - 获取年份最后一天 Date 对象
|
|
539
|
+
|
|
540
|
+
获取指定年份 UTC 最后一天 23:59:59 的 Date 对象。
|
|
541
|
+
|
|
542
|
+
**示例:**
|
|
543
|
+
|
|
544
|
+
```typescript
|
|
545
|
+
getUTCYearEnd(2024)
|
|
546
|
+
// Date 对象: 2024-12-31 23:59:59 UTC
|
|
547
|
+
|
|
548
|
+
getUTCYearEnd()
|
|
549
|
+
// 当前年份最后一天
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
### getUTCWeeksInYear - 获取一年有多少周
|
|
553
|
+
|
|
554
|
+
获取指定年份有多少周(ISO 8601 标准,一年有 52 或 53 周)。
|
|
555
|
+
|
|
556
|
+
**参数:**
|
|
557
|
+
- `year`: 年份,默认当前年份
|
|
558
|
+
|
|
559
|
+
**返回:** 周数(52 或 53)
|
|
560
|
+
|
|
561
|
+
**示例:**
|
|
562
|
+
|
|
563
|
+
```typescript
|
|
564
|
+
getUTCWeeksInYear(2024)
|
|
565
|
+
// 52
|
|
566
|
+
|
|
567
|
+
getUTCWeeksInYear(2020)
|
|
568
|
+
// 53
|
|
569
|
+
|
|
570
|
+
getUTCWeeksInYear()
|
|
571
|
+
// 当前年份的周数
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### getUTCWeekStart - 获取指定周的开始时间
|
|
575
|
+
|
|
576
|
+
获取指定年份指定周的开始时间(周一 00:00:00 UTC)。
|
|
577
|
+
|
|
578
|
+
**参数:**
|
|
579
|
+
- `year`: 年份
|
|
580
|
+
- `week`: 周数(1-53)
|
|
581
|
+
|
|
582
|
+
**返回:** UTC Date 对象
|
|
583
|
+
|
|
584
|
+
**示例:**
|
|
585
|
+
|
|
586
|
+
```typescript
|
|
587
|
+
// 获取 2024 年第 1 周的开始时间
|
|
588
|
+
getUTCWeekStart(2024, 1)
|
|
589
|
+
// 2024-01-01 00:00:00 UTC (周一)
|
|
590
|
+
|
|
591
|
+
// 获取 2024 年第 10 周的开始时间
|
|
592
|
+
getUTCWeekStart(2024, 10)
|
|
593
|
+
// 2024-03-04 00:00:00 UTC (周一)
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
### getUTCWeekEnd - 获取指定周的结束时间
|
|
597
|
+
|
|
598
|
+
获取指定年份指定周的结束时间(周日 23:59:59 UTC)。
|
|
599
|
+
|
|
600
|
+
**参数:**
|
|
601
|
+
- `year`: 年份
|
|
602
|
+
- `week`: 周数(1-53)
|
|
603
|
+
|
|
604
|
+
**返回:** UTC Date 对象
|
|
605
|
+
|
|
606
|
+
**示例:**
|
|
607
|
+
|
|
608
|
+
```typescript
|
|
609
|
+
// 获取 2024 年第 1 周的结束时间
|
|
610
|
+
getUTCWeekEnd(2024, 1)
|
|
611
|
+
// 2024-01-07 23:59:59 UTC (周日)
|
|
612
|
+
|
|
613
|
+
// 获取 2024 年第 10 周的结束时间
|
|
614
|
+
getUTCWeekEnd(2024, 10)
|
|
615
|
+
// 2024-03-10 23:59:59 UTC (周日)
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
### getUTCAllWeeksInYear - 获取一年所有周的时间范围
|
|
619
|
+
|
|
620
|
+
获取指定年份所有周的开始和结束时间。
|
|
621
|
+
|
|
622
|
+
**参数:**
|
|
623
|
+
- `year`: 年份,默认当前年份
|
|
624
|
+
|
|
625
|
+
**返回:** 包含所有周信息的数组
|
|
626
|
+
|
|
627
|
+
**示例:**
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
const weeks = getUTCAllWeeksInYear(2024)
|
|
631
|
+
console.log(weeks)
|
|
632
|
+
/*
|
|
633
|
+
[
|
|
634
|
+
{
|
|
635
|
+
week: 1,
|
|
636
|
+
start: Date (2024-01-01 00:00:00 UTC),
|
|
637
|
+
end: Date (2024-01-07 23:59:59 UTC),
|
|
638
|
+
startTimestamp: 1704067200000,
|
|
639
|
+
endTimestamp: 1704671999999
|
|
640
|
+
},
|
|
641
|
+
{
|
|
642
|
+
week: 2,
|
|
643
|
+
start: Date (2024-01-08 00:00:00 UTC),
|
|
644
|
+
end: Date (2024-01-14 23:59:59 UTC),
|
|
645
|
+
startTimestamp: 1704672000000,
|
|
646
|
+
endTimestamp: 1705276799999
|
|
647
|
+
},
|
|
648
|
+
// ... 共 52 或 53 周
|
|
649
|
+
]
|
|
650
|
+
*/
|
|
651
|
+
|
|
652
|
+
// 遍历所有周
|
|
653
|
+
weeks.forEach(({ week, start, end }) => {
|
|
654
|
+
console.log(`第 ${week} 周: ${formatUTC(start)} 至 ${formatUTC(end)}`)
|
|
655
|
+
})
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
### getUTCWeekNumber - 获取日期是第几周
|
|
659
|
+
|
|
660
|
+
获取指定日期是 UTC 时区的第几周(ISO 8601 标准)。
|
|
661
|
+
|
|
662
|
+
**参数:**
|
|
663
|
+
- `date`: 日期对象、时间戳或日期字符串
|
|
664
|
+
|
|
665
|
+
**返回:** 周数
|
|
666
|
+
|
|
667
|
+
**示例:**
|
|
668
|
+
|
|
669
|
+
```typescript
|
|
670
|
+
getUTCWeekNumber(new Date('2024-01-01'))
|
|
671
|
+
// 1
|
|
672
|
+
|
|
673
|
+
getUTCWeekNumber(new Date('2024-03-15'))
|
|
674
|
+
// 11
|
|
675
|
+
|
|
676
|
+
getUTCWeekNumber(new Date())
|
|
677
|
+
// 当前是第几周
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
## 更多应用场景
|
|
681
|
+
|
|
682
|
+
### 年度数据统计
|
|
683
|
+
|
|
684
|
+
```typescript
|
|
685
|
+
import { getUTCYearStartTimestamp, getUTCYearEndTimestamp } from 'your-package-name/date/utc'
|
|
686
|
+
|
|
687
|
+
// 获取 2024 年全年数据
|
|
688
|
+
const yearStart = getUTCYearStartTimestamp(2024)
|
|
689
|
+
const yearEnd = getUTCYearEndTimestamp(2024)
|
|
690
|
+
|
|
691
|
+
fetchYearlyData({
|
|
692
|
+
startTimestamp: yearStart,
|
|
693
|
+
endTimestamp: yearEnd
|
|
694
|
+
})
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
### 周报表生成
|
|
698
|
+
|
|
699
|
+
```typescript
|
|
700
|
+
import { getUTCWeekStart, getUTCWeekEnd, formatUTC } from 'your-package-name/date/utc'
|
|
701
|
+
|
|
702
|
+
// 生成第 10 周的报表
|
|
703
|
+
const weekStart = getUTCWeekStart(2024, 10)
|
|
704
|
+
const weekEnd = getUTCWeekEnd(2024, 10)
|
|
705
|
+
|
|
706
|
+
console.log(`第 10 周报表`)
|
|
707
|
+
console.log(`时间范围: ${formatUTC(weekStart)} 至 ${formatUTC(weekEnd)}`)
|
|
708
|
+
|
|
709
|
+
fetchWeeklyReport({
|
|
710
|
+
startDate: weekStart,
|
|
711
|
+
endDate: weekEnd
|
|
712
|
+
})
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
### 获取当前周数
|
|
716
|
+
|
|
717
|
+
```typescript
|
|
718
|
+
import { getUTCWeekNumber } from 'your-package-name/date/utc'
|
|
719
|
+
|
|
720
|
+
const currentWeek = getUTCWeekNumber(new Date())
|
|
721
|
+
console.log(`当前是 2024 年第 ${currentWeek} 周`)
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
### 生成全年周历
|
|
725
|
+
|
|
726
|
+
```typescript
|
|
727
|
+
import { getUTCAllWeeksInYear, formatUTC } from 'your-package-name/date/utc'
|
|
728
|
+
|
|
729
|
+
const weeks = getUTCAllWeeksInYear(2024)
|
|
730
|
+
|
|
731
|
+
// 生成周历表格
|
|
732
|
+
weeks.forEach(({ week, start, end }) => {
|
|
733
|
+
console.log(`第 ${week} 周: ${formatUTC(start, 'MM-dd')} ~ ${formatUTC(end, 'MM-dd')}`)
|
|
734
|
+
})
|
|
735
|
+
|
|
736
|
+
/*
|
|
737
|
+
第 1 周: 01-01 ~ 01-07
|
|
738
|
+
第 2 周: 01-08 ~ 01-14
|
|
739
|
+
第 3 周: 01-15 ~ 01-21
|
|
740
|
+
...
|
|
741
|
+
第 52 周: 12-23 ~ 12-29
|
|
742
|
+
*/
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
### 按周统计数据
|
|
746
|
+
|
|
747
|
+
```typescript
|
|
748
|
+
import { getUTCWeeksInYear, getUTCWeekStart, getUTCWeekEnd } from 'your-package-name/date/utc'
|
|
749
|
+
|
|
750
|
+
const year = 2024
|
|
751
|
+
const weeksCount = getUTCWeeksInYear(year)
|
|
752
|
+
|
|
753
|
+
// 获取每周的数据
|
|
754
|
+
for (let week = 1; week <= weeksCount; week++) {
|
|
755
|
+
const weekStart = getUTCWeekStart(year, week)
|
|
756
|
+
const weekEnd = getUTCWeekEnd(year, week)
|
|
757
|
+
|
|
758
|
+
const data = await fetchWeeklyData({
|
|
759
|
+
startTimestamp: weekStart.getTime(),
|
|
760
|
+
endTimestamp: weekEnd.getTime()
|
|
761
|
+
})
|
|
762
|
+
|
|
763
|
+
console.log(`第 ${week} 周数据:`, data)
|
|
764
|
+
}
|
|
765
|
+
```
|
|
766
|
+
|
|
767
|
+
### 判断日期所在周
|
|
768
|
+
|
|
769
|
+
```typescript
|
|
770
|
+
import { getUTCWeekNumber, getUTCWeekStart, getUTCWeekEnd, formatUTC } from 'your-package-name/date/utc'
|
|
771
|
+
|
|
772
|
+
const date = new Date('2024-03-15')
|
|
773
|
+
const weekNumber = getUTCWeekNumber(date)
|
|
774
|
+
const weekStart = getUTCWeekStart(2024, weekNumber)
|
|
775
|
+
const weekEnd = getUTCWeekEnd(2024, weekNumber)
|
|
776
|
+
|
|
777
|
+
console.log(`${formatUTC(date, 'yyyy-MM-dd')} 是第 ${weekNumber} 周`)
|
|
778
|
+
console.log(`该周范围: ${formatUTC(weekStart, 'yyyy-MM-dd')} 至 ${formatUTC(weekEnd, 'yyyy-MM-dd')}`)
|
|
779
|
+
```
|