( date: DateArg<DateType>, month: number, options?: SetMonthOptions<ResultDate> | undefined, )
| 33 | * //=> Sat Feb 01 2014 00:00:00 |
| 34 | */ |
| 35 | export function setMonth< |
| 36 | DateType extends Date, |
| 37 | ResultDate extends Date = DateType, |
| 38 | >( |
| 39 | date: DateArg<DateType>, |
| 40 | month: number, |
| 41 | options?: SetMonthOptions<ResultDate> | undefined, |
| 42 | ): ResultDate { |
| 43 | const _date = toDate(date, options?.in); |
| 44 | const year = _date.getFullYear(); |
| 45 | const day = _date.getDate(); |
| 46 | |
| 47 | const midMonth = constructFrom(options?.in || date, 0); |
| 48 | midMonth.setFullYear(year, month, 15); |
| 49 | midMonth.setHours(0, 0, 0, 0); |
| 50 | const daysInMonth = getDaysInMonth(midMonth); |
| 51 | |
| 52 | // Set the earlier date, allows to wrap Jan 31 to Feb 28 |
| 53 | _date.setMonth(month, Math.min(day, daysInMonth)); |
| 54 | return _date; |
| 55 | } |
no test coverage detected