@jsbits/add-months
Version:
Adds or subtracts N months to any JavaScript Date, local or UTC.
1 lines • 5.12 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["index.ts"],"sourcesContent":["/**\n * Increment or decrement the given date by `count` months.\n *\n * @param {Date} date Local date\n * @param {number} count Months to add or subtract\n * @returns {Date} New local date\n * @private\n */\nconst addMonthsLoc = (date: Date, count: number) => {\n // Get the day of this date, we just need to take care of days >28\n const day = date.getDate()\n\n // Set the month and change the day if it's above 28 to avoid overflow\n date.setMonth(date.getMonth() + count, day > 28 ? 28 : day)\n\n // Restore the day if necessary, preserving the month and we are done.\n if (day > 28) {\n const month = date.getMonth()\n /*\n \"If there is an overflow in the day, the date is adjusted to the last\n valid day of the expected month.\"\n */\n date.setDate(day)\n if (date.getMonth() !== month) {\n date.setDate(0)\n }\n }\n\n return date\n}\n\n/**\n * Like the `addMonthsLoc` function, but using UTC methods.\n *\n * @param {Date} date UTC date\n * @param {number} count Months to add or subtract\n * @returns {Date} New UTC date\n * @private\n */\nconst addMonthsUTC = (date: Date, count: number) => {\n const day = date.getUTCDate()\n\n date.setUTCMonth(date.getUTCMonth() + count, day > 28 ? 28 : day)\n\n if (day > 28) {\n const month = date.getUTCMonth()\n\n date.setUTCDate(day)\n if (date.getUTCMonth() !== month) {\n date.setUTCDate(0)\n }\n }\n\n return date\n}\n\n/** Shortcut */\nconst _toString = Object.prototype.toString\n\n/**\n * Convert a number or Date object to a Date, for other types, returns\n * an invalid date (i.e. NaN).\n *\n * @param {*} src Date or number\n * @return {Date} Parsed date\n * @private\n */\nconst toDate = (src: any) => {\n const type = _toString.call(src)\n\n return new Date(type === '[object Date]' || type === '[object Number]' ? +src : NaN)\n}\n\n/**\n * Returns a date occurring `count` months after `startdate` or, if `count` is\n * negative, the date occurring `count` months before `startdate`.\n *\n * - If `startdate` is not a Date or number that can be converted to a\n * valid date, returns a new Date instance with an invalid date.\n *\n * - If `count` is evaluated as zero, returns a new Date instance with the\n * the same value as `startdate`.\n *\n * - If there is an overflow in the day, the date is adjusted to the last\n * valid day of the expected month.\n *\n * The third parameter is optional and indicates if the date is UTC. It is\n * necessary to differentiate UTC dates from locals and avoid errors due to the\n * [Daylight Saving Time](https://en.wikipedia.org/wiki/Daylight_saving_time)\n * (DST).\n *\n * This function does not change the original date.\n *\n * @param {Date|number} startdate A value parseable as a JavaScript Date\n * @param {number} count Number of months to add or subtract\n * @param {boolean} [asUTC=false] If `true`, handle the date as UTC\n * @returns {Date} A new, adjusted Date instance.\n * @since 1.0.0\n */\nconst addMonths = function _addMonths (startdate: Date | number, count: number, asUTC?: boolean) {\n\n // Create a new `Date` instance.\n // Wrong types return NaN dates without throwing exceptions.\n const date = toDate(startdate)\n\n // Corce `count` with 'ToInt32'\n count |= 0\n\n // Filter out zero count and invalid dates.\n if (!count || isNaN(date as any)) {\n return date\n }\n\n // Return the result with the UTC methods if required by the flags\n return asUTC ? addMonthsUTC(date, count) : addMonthsLoc(date, count)\n}\n\nexport = addMonths\n"],"names":[],"mappings":";;;;;;;;AAQA,MAAM,YAAY,GAAG,CAAC,IAAU,EAAE,KAAa;IAE7C,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;IAG1B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,KAAK,EAAE,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,CAAA;IAG3D,IAAI,GAAG,GAAG,EAAE,EAAE;QACZ,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAA;QAK7B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QACjB,IAAI,IAAI,CAAC,QAAQ,EAAE,KAAK,KAAK,EAAE;YAC7B,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;SAChB;KACF;IAED,OAAO,IAAI,CAAA;CACZ,CAAA;AAUD,MAAM,YAAY,GAAG,CAAC,IAAU,EAAE,KAAa;IAC7C,MAAM,GAAG,GAAG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