-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateUtils.ts
33 lines (30 loc) · 1.01 KB
/
dateUtils.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
export function getDateAddYear(yearDiff: number): Date {
const now = new Date();
now.setFullYear(now.getFullYear() + yearDiff);
return now;
}
export function dateToString(date: Date, locale = 'en-US') {
return [
date.getFullYear(),
(date.getMonth() + 1).toLocaleString(locale, { minimumIntegerDigits: 2 }),
date.getDate().toLocaleString(locale, { minimumIntegerDigits: 2 }),
].join('-');
}
// https://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript
// For webkit :/
export function isValidDate(date: Date) {
return !isNaN(date.getTime());
}
export function stringToDate(date = '', locale = 'en-US') {
const dateArr = date.split('-');
if (dateArr.length === 3) {
try {
const d = new Date([dateArr[0],
parseInt(dateArr[1], 10).toLocaleString(locale, { minimumIntegerDigits: 2 }),
parseInt(dateArr[2], 10).toLocaleString(locale, { minimumIntegerDigits: 2 })
].join('-'));
return d;
} catch {}
}
return new Date(date);
}