/
index.js
36 lines (35 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import toDate from '../toDate/index.js'
import getDaysInMonth from '../getDaysInMonth/index.js'
/**
* @name addMonths
* @category Month Helpers
* @summary Add the specified number of months to the given date.
*
* @description
* Add the specified number of months to the given date.
*
* @param {Date|String|Number} date - the date to be changed
* @param {Number} amount - the amount of months to be added
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link docs/toDate}
* @returns {Date} the new date with the months added
* @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
*
* @example
* // Add 5 months to 1 September 2014:
* var result = addMonths(new Date(2014, 8, 1), 5)
* //=> Sun Feb 01 2015 00:00:00
*/
export default function addMonths (dirtyDate, dirtyAmount, dirtyOptions) {
var date = toDate(dirtyDate, dirtyOptions)
var amount = Number(dirtyAmount)
var desiredMonth = date.getMonth() + amount
var dateWithDesiredMonth = new Date(0)
dateWithDesiredMonth.setFullYear(date.getFullYear(), desiredMonth, 1)
dateWithDesiredMonth.setHours(0, 0, 0, 0)
var daysInMonth = getDaysInMonth(dateWithDesiredMonth, dirtyOptions)
// Set the last day of the new month
// if the original date was the last day of the longer month
date.setMonth(desiredMonth, Math.min(daysInMonth, date.getDate()))
return date
}