Get the first and last date of the previous month in JQuery

前端 未结 7 788
攒了一身酷
攒了一身酷 2021-02-09 07:41

I have this script,

 var today = new Date();
 var dd = today.getDate();
 var ffffd = today.getDate()-1;
 var ffffdd = today.getDate()-2;

 var mm = today.getMonth()+         


        
7条回答
  •  执念已碎
    2021-02-09 08:18

    A convenient trick to get the end of last month is the take current date and use setDate(0). Then use that that to create a new date object and setDate(1) for beginning of the month

    var prevMonthEnd = new Date();
    prevMonthEnd.setDate(0);
    var beginLastMonth = new Date(prevMonthEnd);
    beginLastMonth.setDate(1);
    
    
    console.log([beginLastMonth.toString(), prevMonthEnd.toString()])

提交回复
热议问题