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