1 | import { toDate } from "./toDate.mjs";
|
2 | import { constructFrom } from "./constructFrom.mjs";
|
3 |
|
4 | /**
|
5 | * @name addMonths
|
6 | * @category Month Helpers
|
7 | * @summary Add the specified number of months to the given date.
|
8 | *
|
9 | * @description
|
10 | * Add the specified number of months to the given date.
|
11 | *
|
12 | * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
13 | *
|
14 | * @param date - The date to be changed
|
15 | * @param amount - The amount of months to be added.
|
16 | *
|
17 | * @returns The new date with the months added
|
18 | *
|
19 | * @example
|
20 | * // Add 5 months to 1 September 2014:
|
21 | * const result = addMonths(new Date(2014, 8, 1), 5)
|
22 | * //=> Sun Feb 01 2015 00:00:00
|
23 | *
|
24 | * // Add one month to 30 January 2023:
|
25 | * const result = addMonths(new Date(2023, 0, 30), 1)
|
26 | * //=> Tue Feb 28 2023 00:00:00
|
27 | */
|
28 | export function addMonths(date, amount) {
|
29 | const _date = toDate(date);
|
30 | if (isNaN(amount)) return constructFrom(date, NaN);
|
31 | if (!amount) {
|
32 | // If 0 months, no-op to avoid changing times in the hour before end of DST
|
33 | return _date;
|
34 | }
|
35 | const dayOfMonth = _date.getDate();
|
36 |
|
37 | // The JS Date object supports date math by accepting out-of-bounds values for
|
38 | // month, day, etc. For example, new Date(2020, 0, 0) returns 31 Dec 2019 and
|
39 | // new Date(2020, 13, 1) returns 1 Feb 2021. This is *almost* the behavior we
|
40 | // want except that dates will wrap around the end of a month, meaning that
|
41 | // new Date(2020, 13, 31) will return 3 Mar 2021 not 28 Feb 2021 as desired. So
|
42 | // we'll default to the end of the desired month by adding 1 to the desired
|
43 | // month and using a date of 0 to back up one day to the end of the desired
|
44 | // month.
|
45 | const endOfDesiredMonth = constructFrom(date, _date.getTime());
|
46 | endOfDesiredMonth.setMonth(_date.getMonth() + amount + 1, 0);
|
47 | const daysInMonth = endOfDesiredMonth.getDate();
|
48 | if (dayOfMonth >= daysInMonth) {
|
49 | // If we're already at the end of the month, then this is the correct date
|
50 | // and we're done.
|
51 | return endOfDesiredMonth;
|
52 | } else {
|
53 | // Otherwise, we now know that setting the original day-of-month value won't
|
54 | // cause an overflow, so set the desired day-of-month. Note that we can't
|
55 | // just set the date of `endOfDesiredMonth` because that object may have had
|
56 | // its time changed in the unusual case where where a DST transition was on
|
57 | // the last day of the month and its local time was in the hour skipped or
|
58 | // repeated next to a DST transition. So we use `date` instead which is
|
59 | // guaranteed to still have the original time.
|
60 | _date.setFullYear(
|
61 | endOfDesiredMonth.getFullYear(),
|
62 | endOfDesiredMonth.getMonth(),
|
63 | dayOfMonth,
|
64 | );
|
65 | return _date;
|
66 | }
|
67 | }
|
68 |
|
69 | // Fallback for modularized imports:
|
70 | export default addMonths;
|