(month: number, year: number, firstDayOfWeek: number, showAdjacentDays = false)
| 103 | * using null values. |
| 104 | */ |
| 105 | export const getDaysOfMonth = (month: number, year: number, firstDayOfWeek: number, showAdjacentDays = false) => { |
| 106 | const numDays = getNumDaysInMonth(month, year); |
| 107 | let previousNumDays: number; //previous month number of days |
| 108 | if (month === 1) { |
| 109 | // If the current month is January, the previous month should be December of the previous year. |
| 110 | previousNumDays = getNumDaysInMonth(12, year - 1); |
| 111 | } else { |
| 112 | // Otherwise, the previous month should be the current month - 1 of the same year. |
| 113 | previousNumDays = getNumDaysInMonth(month - 1, year); |
| 114 | } |
| 115 | const firstOfMonth = new Date(`${month}/1/${year}`).getDay(); |
| 116 | |
| 117 | /** |
| 118 | * To get the first day of the month aligned on the correct |
| 119 | * day of the week, we need to determine how many "filler" days |
| 120 | * to generate. These filler days as empty/disabled buttons |
| 121 | * that fill the space of the days of the week before the first |
| 122 | * of the month. |
| 123 | * |
| 124 | * There are two cases here: |
| 125 | * |
| 126 | * 1. If firstOfMonth = 4, firstDayOfWeek = 0 then the offset |
| 127 | * is (4 - (0 + 1)) = 3. Since the offset loop goes from 0 to 3 inclusive, |
| 128 | * this will generate 4 filler days (0, 1, 2, 3), and then day of week 4 will have |
| 129 | * the first day of the month. |
| 130 | * |
| 131 | * 2. If firstOfMonth = 2, firstDayOfWeek = 4 then the offset |
| 132 | * is (6 - (4 - 2)) = 4. Since the offset loop goes from 0 to 4 inclusive, |
| 133 | * this will generate 5 filler days (0, 1, 2, 3, 4), and then day of week 5 will have |
| 134 | * the first day of the month. |
| 135 | */ |
| 136 | const offset = |
| 137 | firstOfMonth >= firstDayOfWeek ? firstOfMonth - (firstDayOfWeek + 1) : 6 - (firstDayOfWeek - firstOfMonth); |
| 138 | |
| 139 | let days: ( |
| 140 | | { |
| 141 | day: number; |
| 142 | dayOfWeek: number; |
| 143 | isAdjacentDay: boolean; |
| 144 | } |
| 145 | | { |
| 146 | day: null; |
| 147 | dayOfWeek: null; |
| 148 | isAdjacentDay: boolean; |
| 149 | } |
| 150 | )[] = []; |
| 151 | for (let i = 1; i <= numDays; i++) { |
| 152 | days.push({ day: i, dayOfWeek: (offset + i) % 7, isAdjacentDay: false }); |
| 153 | } |
| 154 | |
| 155 | if (showAdjacentDays) { |
| 156 | for (let i = 0; i <= offset; i++) { |
| 157 | // Using offset create previous month adjacent day, starting from last day |
| 158 | days = [{ day: previousNumDays - i, dayOfWeek: (previousNumDays - i) % 7, isAdjacentDay: true }, ...days]; |
| 159 | } |
| 160 | |
| 161 | // Calculate positiveOffset |
| 162 | // The calendar will display 42 days (6 rows of 7 columns) |
no test coverage detected