-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
date.js
55 lines (49 loc) · 1.23 KB
/
date.js
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
export function formatDate(startDate, endDate) {
let formatted = '';
let year, month, day;
const isRange = (startDate.getDate() !== endDate.getDate() ||
startDate.getMonth() !== endDate.getMonth() ||
startDate.getFullYear() !== endDate.getFullYear()
);
const nowYear = new Date().getFullYear();
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
if (isRange) {
month = [
months[startDate.getMonth()],
months[endDate.getMonth()]
];
year = [startDate.getFullYear(), endDate.getFullYear()];
day = [startDate.getDate(), endDate.getDate()];
if (month[0] !== month[1]) {
formatted = month[0] + ' ' + day[0] + '-' + month[1] + ' ' + day[1];
} else {
formatted = month[0] + ' ' + day[0] + '-' + day[1];
}
if (year[0] !== nowYear || year[1] !== nowYear) {
formatted += ', ' + year[1];
}
}
else {
year = startDate.getFullYear();
month = months[startDate.getMonth()];
day = startDate.getDate();
formatted = month + ' ' + day;
if (year !== nowYear) {
formatted += ', ' + year;
}
}
return formatted;
}