This repository was archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathdate-utils.ts
257 lines (211 loc) · 5.57 KB
/
date-utils.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
const ISO_DATE_FORMAT = /^(\d{4})-(\d{2})-(\d{2})$/
export enum DaysOfWeek {
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
}
export class DateInvalidException implements Error {
constructor(message, name, date) {
this.message = message
this.name = name
this.date = date
}
message: string
name: string
date: Date
}
export function createDate(year: string, month: string, day: string): Date {
var dayInt = parseInt(day, 10)
var monthInt = parseInt(month, 10)
var yearInt = parseInt(year, 10)
const isValid =
Number.isInteger(yearInt) && // all parts should be integers
Number.isInteger(monthInt) &&
Number.isInteger(dayInt) &&
monthInt > 0 && // month must be 1-12
monthInt <= 12 &&
dayInt > 0 && // day must be 1-31
dayInt <= 31 &&
yearInt > 0
if (isValid) {
const date = new Date(yearInt, monthInt - 1, dayInt)
if (isDateChanged(date, dayInt, monthInt, yearInt)) {
throw new DateInvalidException("Invalid date", "Invalid date", date)
}
return date
}
}
/**
*
* @param date
* @param day
* @param month
* @param year
*/
function isDateChanged(date: Date, day: number, month: number, year: number): boolean {
if (date.getDate() != day || date.getMonth() + 1 != month || date.getFullYear() != year) {
return true
}
return false
}
/**
* @param value date string in ISO format YYYY-MM-DD
*/
export function parseISODate(value: string): Date {
if (!value) {
return
}
const matches = value.match(ISO_DATE_FORMAT)
if (matches) {
try {
return createDate(matches[1], matches[2], matches[3])
} catch (e) {}
}
}
/**
* print date in format YYYY-MM-DD
* @param date
*/
export function printISODate(date: Date): string {
if (!date) {
return ""
}
var d = date.getDate().toString(10)
var m = (date.getMonth() + 1).toString(10)
var y = date.getFullYear().toString(10)
// days are not zero-indexed, so pad if less than 10
if (date.getDate() < 10) {
d = `0${d}`
}
// months *are* zero-indexed, pad if less than 9!
if (date.getMonth() < 9) {
m = `0${m}`
}
return `${y}-${m}-${d}`
}
/**
* Compare if two dates are equal in terms of day, month, and year
*/
export function isEqual(a: Date, b: Date): boolean {
if (a == null || b == null) {
return false
}
return isEqualMonth(a, b) && a.getDate() === b.getDate()
}
/**
* Compare if two dates are in the same month of the same year.
*/
export function isEqualMonth(a: Date, b: Date): boolean {
if (a == null || b == null) {
return false
}
return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth()
}
export function addDays(date: Date, days: number): Date {
var d = new Date(date)
d.setDate(d.getDate() + days)
return d
}
export function addMonths(date: Date, months: number): Date {
const d = new Date(date)
d.setMonth(date.getMonth() + months)
return d
}
export function addYears(date: Date, years: number): Date {
const d = new Date(date)
d.setFullYear(date.getFullYear() + years)
return d
}
export function startOfWeek(date: Date, firstDayOfWeek: DaysOfWeek = DaysOfWeek.Monday): Date {
var d = new Date(date)
var day = d.getDay()
var diff = (day < firstDayOfWeek ? 7 : 0) + day - firstDayOfWeek
d.setDate(d.getDate() - diff)
return d
}
export function endOfWeek(date: Date, firstDayOfWeek: DaysOfWeek = DaysOfWeek.Monday): Date {
var d = new Date(date)
var day = d.getDay()
var diff = (day < firstDayOfWeek ? -7 : 0) + 6 - (day - firstDayOfWeek)
d.setDate(d.getDate() + diff)
return d
}
export function startOfMonth(date: Date): Date {
return new Date(date.getFullYear(), date.getMonth(), 1)
}
export function endOfMonth(date: Date): Date {
return new Date(date.getFullYear(), date.getMonth() + 1, 0)
}
export function setMonth(date: Date, month: number): Date {
const d = new Date(date)
d.setMonth(month)
return d
}
export function setYear(date: Date, year: number): Date {
const d = new Date(date)
d.setFullYear(year)
return d
}
/**
* Check if date is within a min and max
*/
export function inRange(date: Date, min?: Date, max?: Date): boolean {
return clamp(date, min, max) === date
}
/**
* Ensures date is within range, returns min or max if out of bounds
*/
export function clamp(date: Date, min?: Date, max?: Date): Date {
const time = date.getTime()
if (min && min instanceof Date && time < min.getTime()) {
return min
}
if (max && max instanceof Date && time > max.getTime()) {
return max
}
return date
}
/**
* given start and end date, return an (inclusive) array of all dates in between
* @param start
* @param end
*/
function getDaysInRange(start: Date, end: Date): Date[] {
const days: Date[] = []
let current = start
while (!isEqual(current, end)) {
days.push(current)
current = addDays(current, 1)
}
days.push(current)
return days
}
/**
* given a date, return an array of dates from a calendar perspective
* @param date
* @param firstDayOfWeek
*/
export function getViewOfMonth(date: Date, firstDayOfWeek: DaysOfWeek = DaysOfWeek.Monday): Date[] {
const start = startOfWeek(startOfMonth(date), firstDayOfWeek)
const end = endOfWeek(endOfMonth(date), firstDayOfWeek)
return getDaysInRange(start, end)
}
/**
* Form random hash
*/
export function chr4() {
return Math.random()
.toString(16)
.slice(-4)
}
/**
* Create random identifier with a prefix
* @param prefix
*/
export function createIdentifier(prefix) {
return `${prefix}-${chr4()}${chr4()}-${chr4()}-${chr4()}-${chr4()}-${chr4()}${chr4()}${chr4()}`
}