JS 日期计算 天数加减、月数加减
addDays(date, days) { //给指定日期增加天数 if (!days) { return date; } let dateArr = date.split(' '); date = new Date(new Date(date).setDate(new Date(date).getDate() + days)); var year = date.getFullYear(); var month = date.getMonth() + 1; if (month < 10) { month = '0' + month; } var day = date.getDate(); if (day < 10) { day = '0' + day; } date = year + '-' + month + '-' + day; if (dateArr.length == 1) { return date; } return date + ' ' + dateArr[1]; }
//月份,原日期,增加(或减少) months 月数 addMonth(yearmonth,months){ var arr=yearmonth.split('-');//2020-02,或2020-02-02 var year=parseInt(arr[0]); var month=parseInt(arr[1]); var m=month+months; if(m>0){ var yearnum=parseInt((m-1)/12); month=m%12==0?12:m%12; year+=yearnum; } else if(m<=0){ m=Math.abs(month+months); var yearnum=parseInt((m+month+12)/12); month=m%12==0?12:12-m%12; year-=yearnum; } month=month<10?"0"+month:month; return year+"-"+month; }
标签:月数,加减,month,var,date,JS,day From: https://www.cnblogs.com/wishit/p/18105004