js在当前时间后或前添加或减少几个月

function addMonth(date, offset) {
    if (date instanceof Date && !isNaN(offset)) {
        let givenMonth = date.getMonth();
        let newMonth = givenMonth + offset;
        date.setMonth(newMonth);
        return date;
    }
    throw Error('argument type error');
}

 

转载于:https://www.cnblogs.com/wjs0509/p/11265487.html

你可能感兴趣的:(js在当前时间后或前添加或减少几个月)