UNPKG

abbott-methods

Version:

abbott,methods,method,functions,function

31 lines (29 loc) 1.04 kB
import {timeObject} from '../time/timeObject' import {someYearMonthDays} from '../some/someYearMonthDays' /** * @description 获取number个月后(前)的日期,number可为正可为负 * @param {*} [dateValue] 时间 * @param {number} number 几个月后(前) * @returns {Null|String} */ export const dateApartMonth = (dateValue: any = new Date(), number: number): null | string => { const theObject = timeObject(dateValue) if (theObject) { const totalMonths = +theObject.y * 12 + +theObject.m + +number let day = theObject.d let year = Math.floor(totalMonths / 12) let month = totalMonths % 12 if (month === 0) { year = year - 1 month = 12 } const monthDays = someYearMonthDays(year, month) day = day > monthDays ? monthDays : day const Year = String(year).padStart(4, '0') const Month = String(month).padStart(2, '0') const Day = String(day).padStart(2, '0') return `${Year}-${Month}-${Day}` } else { return null } }