-
Notifications
You must be signed in to change notification settings - Fork 0
/
dates.js
85 lines (72 loc) · 1.48 KB
/
dates.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
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
export const SHOW_YEARS = 25;
const START_YEAR = (function() {
const year = new Date().getFullYear();
return Math.floor(year - (SHOW_YEARS - 1) / 2);
})();
export function getStartYear(year) {
if (year === START_YEAR) {
return START_YEAR;
}
if (year > START_YEAR && year < START_YEAR + SHOW_YEARS) {
return START_YEAR;
}
return START_YEAR + Math.floor((year - START_YEAR) / SHOW_YEARS) * SHOW_YEARS;
}
const DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
export function daysInMonth(year, month) {
if (month === 1 && ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0)) {
return 29;
}
return DAYS_IN_MONTH[month];
}
export function prevMonth({ year, month }) {
if (month === 0) {
return {
year: year - 1,
month: 11
}
}
return {
year: year,
month: month - 1
}
}
export function nextMonth({ year, month }) {
if (month === 11) {
return {
year: year + 1,
month: 0
}
}
return {
year: year,
month: month + 1
}
}
export function prevYear({ year, month }) {
return {
year: year - 1,
month
}
}
export function nextYear({ year, month }) {
return {
year: year + 1,
month
}
}
export function prevYears({ year, month }) {
return {
year: year - SHOW_YEARS,
month
}
}
export function nextYears({ year, month }) {
return {
year: year + SHOW_YEARS,
month
}
}
export function isValidDate(date) {
return date instanceof Date && !isNaN(date);
}