-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathdate.ts
More file actions
51 lines (41 loc) · 1.24 KB
/
date.ts
File metadata and controls
51 lines (41 loc) · 1.24 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
export const toLocaleDate = (datetime: string) => {
const date = new Date(datetime);
if (isNaN(date.getTime())) {
return 'n/a';
}
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
day: 'numeric'
};
return date.toLocaleDateString('en', options);
};
export const toLocaleDateTime = (datetime: string | number) => {
const date = new Date(datetime);
if (isNaN(date.getTime())) {
return 'n/a';
}
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
hourCycle: 'h23'
};
return date.toLocaleDateString('en', options);
};
export const isSameDay = (date1: Date, date2: Date) => {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
};
export const isValidDate = (date: string) => {
return !isNaN(new Date(date).getTime());
};
export const diffDays = (date1: Date, date2: Date) => {
const diffTime = Math.abs(date2.getTime() - date1.getTime());
return Math.floor(diffTime / (1000 * 60 * 60 * 24));
};