(refParts: DatetimeParts, numDays: number)
| 187 | * Currently can only go forward at most 1 month. |
| 188 | */ |
| 189 | export const addDays = (refParts: DatetimeParts, numDays: number) => { |
| 190 | const { month, day, year } = refParts; |
| 191 | if (day === null) { |
| 192 | throw new Error('No day provided'); |
| 193 | } |
| 194 | |
| 195 | const workingParts = { |
| 196 | month, |
| 197 | day, |
| 198 | year, |
| 199 | }; |
| 200 | |
| 201 | const daysInMonth = getNumDaysInMonth(month, year); |
| 202 | workingParts.day = day + numDays; |
| 203 | |
| 204 | /** |
| 205 | * If wrapping to next month |
| 206 | * update days and increment month |
| 207 | */ |
| 208 | if (workingParts.day > daysInMonth) { |
| 209 | workingParts.day -= daysInMonth; |
| 210 | workingParts.month += 1; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * If moving to next year, reset |
| 215 | * month to January and increment year |
| 216 | */ |
| 217 | if (workingParts.month > 12) { |
| 218 | workingParts.month = 1; |
| 219 | workingParts.year += 1; |
| 220 | } |
| 221 | |
| 222 | return workingParts; |
| 223 | }; |
| 224 | |
| 225 | /** |
| 226 | * Given DatetimeParts, generate the previous month. |
no test coverage detected