-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
170 lines (147 loc) · 4.12 KB
/
main.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
DayEnum,
type IDay,
type IMonth,
MonthEnum,
type MonthNumber,
} from './types.ts'
const isLeap = (year: number) => new Date(year, 1, 29).getDate() === 29
export const getDaysInMonth = (year: number, month: MonthNumber): number => {
const daysInMonth = [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (month === MonthEnum.February) {
return isLeap(year) ? 29 : 28
}
return daysInMonth[month - 1]
}
export const getPreviousMonth = (year: number, month: MonthNumber): IMonth => {
if (month === MonthEnum.January) {
return {
month: MonthEnum.December,
year: year - 1,
}
}
return {
month: month - 1,
year,
}
}
export const getNextMonth = (year: number, month: MonthNumber): IMonth => {
if (month === MonthEnum.December) {
return {
month: 1,
year: year + 1,
}
}
return {
month: month + 1,
year,
}
}
export const getPreviousDay = (
year: number,
month: MonthNumber,
day: number,
): IDay => {
if (day === 1) {
const prevMonth = getPreviousMonth(year, month)
const lastDayInPrevMonth = getDaysInMonth(prevMonth.year, prevMonth.month)
const previousDayFromPreviousMonth = new Date(
Date.UTC(prevMonth.year, prevMonth.month - 1, lastDayInPrevMonth),
)
return {
dayInMonth: previousDayFromPreviousMonth.getUTCDate(),
dayInWeek: previousDayFromPreviousMonth.getUTCDay(),
month: prevMonth,
}
}
const previousDay = new Date(Date.UTC(year, month - 1, day - 1))
return {
dayInMonth: previousDay.getUTCDate(),
dayInWeek: previousDay.getUTCDay(),
month: { month, year },
}
}
export const getNextDay = (
year: number,
month: MonthNumber,
day: number,
): IDay => {
const isLastDayInMonth = (y: number, m: MonthNumber, d: number) =>
getDaysInMonth(y, m) === d
if (isLastDayInMonth(year, month, day)) {
const nextMonth = getNextMonth(year, month)
const nextDayOfNextMonth = new Date(
Date.UTC(nextMonth.year, nextMonth.month - 1, 1),
)
return {
dayInMonth: nextDayOfNextMonth.getUTCDate(),
dayInWeek: nextDayOfNextMonth.getUTCDay(),
month: nextMonth,
}
}
const nextDay = new Date(Date.UTC(year, month - 1, day + 1))
return {
dayInMonth: nextDay.getUTCDate(),
dayInWeek: nextDay.getUTCDay(),
month: { month, year },
}
}
/**
* Get calendar for given month.
*
* @param {number} year - year [1900 – 2100]
* @param {number} month - Month [1 - 12]
* @param {number} [startOfTheWeek=0] - Start of the week [0 – 6] where 0 is Sunday, and 6 is Saturday
*
* @returns {Object[][]}
*/
export function calendarMonth(
year: number,
month: number,
startOfTheWeek: number = DayEnum.Sunday,
): IDay[][] {
const firstDayInMonth = new Date(Date.UTC(year, month - 1, 1))
let startingDay: IDay = {
dayInMonth: firstDayInMonth.getUTCDate(),
dayInWeek: firstDayInMonth.getUTCDay(),
month: { month, year },
}
// go back to first start of the week (Monday or Sunday, or any other really)
if (startingDay.dayInWeek !== startOfTheWeek) {
let previousDay = getPreviousDay(
startingDay.month.year,
startingDay.month.month,
startingDay.dayInMonth,
)
while (previousDay.dayInWeek !== startOfTheWeek) {
previousDay = getPreviousDay(
previousDay.month.year,
previousDay.month.month,
previousDay.dayInMonth,
)
}
startingDay = previousDay
}
const daysInWeek = 7
const weeksInCalendarMonth = 6
const calendarMonthDays = daysInWeek * weeksInCalendarMonth
const days: IDay[] = []
let currentDay = startingDay
// then go next day up until all 42 (6 weeks) are filled
while (days.length < calendarMonthDays) {
days.push(currentDay)
currentDay = getNextDay(
currentDay.month.year,
currentDay.month.month,
currentDay.dayInMonth,
)
}
return [
days.slice(daysInWeek * 0, daysInWeek * 1),
days.slice(daysInWeek * 1, daysInWeek * 2),
days.slice(daysInWeek * 2, daysInWeek * 3),
days.slice(daysInWeek * 3, daysInWeek * 4),
days.slice(daysInWeek * 4, daysInWeek * 5),
days.slice(daysInWeek * 5, daysInWeek * 6),
]
}