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');
}

 

posted @ 2019-07-29 17:56  wjs0509  阅读(1877)  评论(0编辑  收藏  举报