-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdateHelpers.ts
37 lines (35 loc) · 1.03 KB
/
dateHelpers.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
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
] as const;
export const formatDateWithMonthString = (
d: Date | string | number | null,
): string => {
// eslint-disable-next-line no-param-reassign
d = new Date(d ?? 0);
if (Number.isNaN(d.getTime()) || d.getTime() <= 0) return '-';
const year = d.getFullYear();
const date = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
const monthIndex = d.getMonth();
const month = months[monthIndex];
return `${date} ${month.slice(0, 3)} ${year}`;
};
export const formatDate = (d: Date | string | number | null): string => {
// eslint-disable-next-line no-param-reassign
d = new Date(d ?? 0);
if (Number.isNaN(d.getTime()) || d.getTime() <= 0) return '-';
const year = d.getFullYear();
const date = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
const month = d.getMonth() < 10 ? `0${d.getMonth()}` : d.getMonth();
return `${year}-${month}-${date}`;
};