-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtime.ts
More file actions
254 lines (233 loc) · 7.08 KB
/
time.ts
File metadata and controls
254 lines (233 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { isRtl } from "./document"
/**
* Copyright (c) 2017-present PanJiaChen
*
* This function is released under the MIT License.
* https://opensource.org/licenses/MIT
*
* Created by PanJiaChen on 16/11/18.
* @see https://github.com/PanJiaChen/vue-element-admin/blob/HEAD/src/utils/index.js
*
* Parse the time to string
*/
export function formatTime(time: Date | string | number, cFormat?: string) {
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date: Date
if (time instanceof Date) {
date = time
} else {
if ((typeof time === 'string')) {
if ((/^[0-9]+$/.test(time))) {
// support "1548221490638"
time = parseInt(time)
} else {
// support safari
time = time.replace(new RegExp(/-/gm), '/')
}
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj: Record<string, number> = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const timeStr = format.replace(/{([ymdhisa])+}/g, (_result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
return value.toString().padStart(2, '0')
})
return timeStr
}
export function formatTimeYMD(time: Date | string | number) {
return formatTime(time, '{y}{m}{d}')
}
/**
* Format milliseconds for display
*/
export function formatPeriod(milliseconds: number, message: Record<'dayMsg' | 'hourMsg' | 'minuteMsg' | 'secondMsg', string>): string {
const prefix = milliseconds < 0 ? '-' : ''
milliseconds = Math.abs(milliseconds)
const { dayMsg, hourMsg, minuteMsg, secondMsg } = message
const seconds = Math.floor(milliseconds / 1000)
const day = Math.floor(seconds / 86400)
const hour = Math.floor(seconds / 3600 - day * 24)
const minute = Math.floor(seconds / 60 - (day * 24 + hour) * 60)
const second = seconds - day * 86400 - hour * 3600 - minute * 60
let msg = dayMsg
if (!day) {
msg = hourMsg
if (!hour) {
msg = minuteMsg
if (!minute) {
msg = secondMsg
}
}
}
const result = msg
.replace('{day}', day.toString())
.replace('{hour}', hour.toString())
.replace('{minute}', minute.toString())
.replace('{second}', second.toString())
return prefix + result
}
/**
* e.g.
*
* 100h0m0s
* 20h10m59s
* 20h0m1s
* 10m20s
* 30s
*
* @return (xx+h)(xx+m)xx+s
*/
export function formatPeriodCommon(milliseconds: number): string {
const defaultMessage = isRtl() ? {
dayMsg: 's {second} m {minute} h {hour} d',
hourMsg: 's {second} m {minute} h {hour}',
minuteMsg: 's {second} m {minute}',
secondMsg: 's {second}',
} : {
dayMsg: '{day} d {hour} h {minute} m {second} s',
hourMsg: '{hour} h {minute} m {second} s',
minuteMsg: '{minute} m {second} s',
secondMsg: '{second} s',
}
return formatPeriod(milliseconds, defaultMessage)
}
export const MILL_PER_SECOND = 1000
export const MILL_PER_MINUTE = MILL_PER_SECOND * 60
export const MILL_PER_HOUR = MILL_PER_MINUTE * 60
/**
* Milliseconds per day
*
* @since 0.0.8
*/
export const MILL_PER_DAY = MILL_PER_HOUR * 24
export const MILL_PER_WEEK = MILL_PER_DAY * 7
/**
* Date range between {start} days ago and {end} days ago
*/
export const daysAgo = (start: number, end: number): [Date, Date] => {
const current = new Date().getTime()
return [new Date(current - start * MILL_PER_DAY), new Date(current - end * MILL_PER_DAY)]
}
export function isSameDay(a: Date, b: Date): boolean {
return a.getFullYear() === b.getFullYear()
&& a.getMonth() === b.getMonth()
&& a.getDate() === b.getDate()
}
/**
* @returns 0 to 6, means Monday to Sunday
*/
export function getWeekDay(date: Date): number {
return (date.getDay() + 6) % 7
}
/**
* Get the start time and end time of this month
*
* @param target the specific time
* @returns [startTime, endTime]
*
* @since 0.6.0
*/
export function getMonthTime(target: Date): [Date, Date] {
const currentMonth = target.getMonth()
const currentYear = target.getFullYear()
const start = new Date(currentYear, currentMonth, 1)
const endTime = new Date(currentYear, currentMonth + 1, 1).getTime()
const end = new Date(endTime - 1)
return [start, end]
}
/**
* Get the start time of this day
*
* @param target the specific time
* @returns the start of this day
* @since 1.0.0
*/
export function getStartOfDay(target: Date) {
const currentMonth = target.getMonth()
const currentYear = target.getFullYear()
const currentDate = target.getDate()
return new Date(currentYear, currentMonth, currentDate)
}
/**
* The birthday of this extension
*
* @since 1.2.0
*/
export function getBirthday(): Date {
const date = new Date()
// 2022-03-03
date.setFullYear(2021)
date.setMonth(2)
date.setDate(3)
date.setHours(0, 0, 0, 0)
return date
}
export const BIRTHDAY = '20220303'
/**
* Calc the day length
* @returns
* 1 if 2022-06-09 00:00:00 to 2022-06-09 00:00:01
* 0 if 2022-06-10 00:00:00 to 2022-06-09 00:00:01
* 2 if 2022-11-10 08:00:00 to 2022-11-11 00:00:01
*/
export function getDayLength(dateStart: Date, dateEnd: Date): number {
let cursor = new Date(dateStart)
let dateDiff = 0
do {
dateDiff += 1
cursor = new Date(cursor.getTime() + MILL_PER_DAY)
} while (cursor.getTime() < dateEnd.getTime())
isSameDay(cursor, dateEnd) && dateDiff++
return dateDiff
}
/**
* Calc the dates between {@param dateStart} and {@param dateEnd}
*
* @returns
* [20220609] if 2022-06-09 00:00:00 to 2022-06-09 00:00:01
* [] if 2022-06-10 00:00:00 to 2022-06-09 00:00:01
* [20221110, 20221111] if 2022-11-10 08:00:00 to 2022-11-11 00:00:01
*/
export function getAllDatesBetween(dateStart: Date, dateEnd: Date): string[] {
let cursor = new Date(dateStart)
let dates: string[] = []
do {
dates.push(formatTimeYMD(cursor))
cursor = new Date(cursor.getTime() + MILL_PER_DAY)
} while (cursor.getTime() < dateEnd.getTime())
isSameDay(cursor, dateEnd) && dates.push(formatTimeYMD(dateEnd))
return dates
}
/**
* yyyyMMdd => Date
*/
export function parseTime(dateStr: string | undefined): Date | undefined {
if (!dateStr) return undefined
const year = parseInt(dateStr.substring(0, 4))
const month = parseInt(dateStr.substring(4, 6))
const date = parseInt(dateStr.substring(6, 8))
const result = new Date()
result.setFullYear(year)
result.setMonth(month - 1)
result.setDate(date)
return result
}