MCPcopy
hub / github.com/date-fns/date-fns / setMonth

Function setMonth

pkgs/core/src/setMonth/index.ts:35–55  ·  view source on GitHub ↗
(
  date: DateArg<DateType>,
  month: number,
  options?: SetMonthOptions<ResultDate> | undefined,
)

Source from the content-addressed store, hash-verified

33 * //=> Sat Feb 01 2014 00:00:00
34 */
35export 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}

Callers 3

setQuarterFunction · 0.90
test.tsFile · 0.90
setFunction · 0.90

Calls 3

toDateFunction · 0.90
constructFromFunction · 0.90
getDaysInMonthFunction · 0.90

Tested by

no test coverage detected