js gets the first and last day of the next month of the specified date

Whether it is to get the previous month or the next month of the specified date, or the previous month or the next month of the current month, you can make a slight change

The following example calculates the first day and the last day of the next month of the specified date (if you want to get the next month or last month of the current month, you don’t need to pass in date)

setDate(date){
    let curDate = new Date(date);
    let y = curDate.getFullYear();
    let m = curDate.getMonth() + 2; // 本身就得+1才等于当前月份,然而我要计算下一个月,所以直接+2
    if (m > 12) {
     m = 1;
     y++
    }
    let monthLastDay = new Date(y, m, 0).getDate();
    return [y + '-' + (m < 10 ? '0' + m : m) + '-' + '01', y + '-' + (m < 10 ? '0' + m : m) + '-' + (monthLastDay < 10 ? '0' + monthLastDay : monthLastDay)]
 },

Call: this.setDate('2022-10-09')

result:

 

Guess you like

Origin blog.csdn.net/weixin_50114203/article/details/127225684