UNPKG

@jsbits/add-months

Version:

Adds or subtracts N months to any JavaScript Date, local or UTC.

57 lines (56 loc) 1.72 kB
/* @jsbits/add-months @author aMarCruz @version 1.1.2 UMD+ES5 @license MIT */ /* eslint-disable */ ;(function(root, factory) { if (typeof define == 'function' && define.amd) { define([], factory) } else if (typeof module == 'object' && module.exports) { module.exports = factory() } else { ((root.$ || root.jQuery || {}).jsbits || root.jsbits || (root.jsbits={})).addMonths = factory() } })(typeof self !== 'undefined' ? self : this, function () { "use strict"; var addMonthsLoc = function (date, count) { var day = date.getDate(); date.setMonth(date.getMonth() + count, day > 28 ? 28 : day); if (day > 28) { var month = date.getMonth(); date.setDate(day); if (date.getMonth() !== month) { date.setDate(0); } } return date; }; var addMonthsUTC = function (date, count) { var day = date.getUTCDate(); date.setUTCMonth(date.getUTCMonth() + count, day > 28 ? 28 : day); if (day > 28) { var month = date.getUTCMonth(); date.setUTCDate(day); if (date.getUTCMonth() !== month) { date.setUTCDate(0); } } return date; }; var _toString = Object.prototype.toString; var toDate = function (src) { var type = _toString.call(src); return new Date(type === '[object Date]' || type === '[object Number]' ? +src : NaN); }; var addMonths = function _addMonths(startdate, count, asUTC) { var date = toDate(startdate); count |= 0; if (!count || isNaN(date)) { return date; } return asUTC ? addMonthsUTC(date, count) : addMonthsLoc(date, count); }; return addMonths; });