-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
date.ts
266 lines (202 loc) · 5.59 KB
/
date.ts
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
255
256
257
258
259
260
261
262
263
264
265
266
import dayjs, { ConfigType, Dayjs, UnitType } from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import localizedFormat from 'dayjs/plugin/localizedFormat'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
dayjs.extend(localizedFormat)
export type Dateable = {
timezone: string
addMonth (): Dateable
addMonths (months: number): Dateable
addDay (): Dateable
addDays (days: number): Dateable
subMonth (): Dateable
subMonths (months: number): Dateable
subDay (): Dateable
subDays (days: number): Dateable
getMonthDays (): number
getYear (): number
getMonth (): number
getDay (): number
getDayOfWeek (): number
getTime (timezone?: string): string
getHours (): number
getMinutes (): number
getSeconds (): number
getNativeDate (): Date
setYear (year: number): Dateable
setMonth (month: number): Dateable
setDay (day: number): Dateable
setTime (time: string): Dateable
setHours (hours: number): Dateable
setMinutes (minutes: number): Dateable
setSeconds (seconds: number): Dateable
setTimezone (timezone: string): Dateable
format (format: string, timezone?: string): string
clone (): Dateable
isValid (): boolean
isInvalid (): boolean
isBefore (date: Dateable | string, unit?: UnitType): boolean
isSame (date: Dateable | string, unit?: UnitType): boolean
isAfter (date: Dateable | string, unit?: UnitType): boolean
toJson (): string
}
export class FluentDate implements Dateable {
timezone: string
private date: Dayjs
constructor (date: ConfigType, timezone = 'UTC', format = '') {
this.timezone = timezone
this.date = format
? dayjs.tz(date, format, timezone)
: dayjs.tz(date, timezone)
}
addDay (): Dateable {
this.date = this.date.add(1, 'day')
return this
}
addDays (days: number): Dateable {
this.date = this.date.add(days, 'day')
return this
}
addMonth (): Dateable {
this.date = this.date.add(1, 'month')
return this
}
addMonths (months: number): Dateable {
this.date = this.date.add(months, 'month')
return this
}
subMonth (): Dateable {
this.date = this.date.subtract(1, 'month')
return this
}
subMonths (months: number): Dateable {
this.date = this.date.subtract(months, 'month')
return this
}
subDay (): Dateable {
this.date = this.date.subtract(1, 'day')
return this
}
subDays (days: number): Dateable {
this.date = this.date.subtract(days, 'day')
return this
}
getMonthDays (): number {
return this.date.daysInMonth()
}
getYear (): number {
return this.date.year()
}
getMonth (): number {
return this.date.month()
}
getDay (): number {
return this.date.date()
}
getDayOfWeek (): number {
return this.date.day()
}
getTime (timezone?: string): string {
if (timezone) {
return this.clone().setTimezone(timezone).getTime()
}
return this.date.format('HH:mm')
}
getHours (): number {
return this.date.get('hours')
}
getMinutes (): number {
return this.date.get('minutes')
}
getSeconds (): number {
return this.date.get('seconds')
}
getNativeDate (): Date {
return this.date.toDate()
}
setYear (year: number): Dateable {
this.date = this.date.set('year', year)
return this
}
setMonth (month: number): Dateable {
this.date = this.date.set('month', month)
return this
}
setDay (day: number): Dateable {
this.date = this.date.set('date', day)
return this
}
setTime (time: string): Dateable {
const [hours = 0, minutes = 0] = time.split(':')
this.setHours(Number(hours))
this.setMinutes(Number(minutes))
return this
}
setHours (hours: number): Dateable {
this.date = this.date.set('hours', hours)
return this
}
setMinutes (minutes: number): Dateable {
this.date = this.date.set('minutes', minutes)
return this
}
setSeconds (seconds: number): Dateable {
this.date = this.date.set('seconds', seconds)
return this
}
setTimezone (timezone: string): Dateable {
this.date = this.date.tz(timezone)
this.timezone = timezone
return this
}
format (format: string, timezone?: string): string {
if (timezone) {
return this.clone().setTimezone(timezone).format(format)
}
return this.date.format(format)
}
clone (): Dateable {
return new FluentDate(this.date, this.timezone)
}
isValid (): boolean {
return this.date.isValid()
}
isInvalid (): boolean {
return !this.isValid()
}
isBefore (date: Dateable | string, unit?: UnitType): boolean {
if (date instanceof FluentDate) {
return this.date.isBefore(date.date, unit)
}
return this.date.isBefore(String(date), unit)
}
isSame (date: Dateable | string, unit?: UnitType): boolean {
if (date instanceof FluentDate) {
return this.date.isSame(date.date, unit)
}
return this.date.isSame(String(date), unit)
}
isAfter (date: Dateable | string, unit?: UnitType): boolean {
if (date instanceof FluentDate) {
return this.date.isAfter(date.date, unit)
}
return this.date.isAfter(String(date), unit)
}
toJson (): string {
return this.date.toJSON()
}
}
export const getLocalTimezone = (): string => {
return dayjs.tz.guess()
}
export interface ParseDate {
(date: ConfigType, timezone?: string, format?: string): Dateable
}
export const date: ParseDate = (date: ConfigType, timezone = 'UTC', format = ''): Dateable => {
return new FluentDate(date, timezone, format)
}
export default FluentDate