计算当前日期前一个月日期:

 1、getThrityMonth(){
  var thrityMonth =  []
           for(var i = 0;i<30;i++){
                thrityMonth.unshift(new Date(new Date()
                    .setDate(new Date().getDate()-i)).toLocaleDateString())
            }
     },
2、
// 获取上一个月时间,返回yyyy-MM-dd字符串
        getLastMonthTime(date){
            //  1    2    3    4    5    6    7    8    9   10    11   12月
            var daysInMonth = [0,31,28,31,30,31,30,31,31,30,31,30,31];
            let timeDate = new Date()
            var strYear = timeDate.getFullYear();
            var strDay = timeDate.getDate();
            var strMonth = timeDate.getMonth()+1;
            //一、解决闰年平年的二月份天数   //平年28天、闰年29天//能被4整除且不能被100整除的为闰年,或能被100整除且能被400整除
            if (((strYear / 4) === 0) && ((strYear / 100)!==0) || ((strYear / 400)===0)){
                daysInMonth[2] = 29;
            }
            if(strMonth - 1 === 0) //二、解决跨年问题
            {
                strYear -= 1;
                strMonth = 12;
            }
            else
            {
                strMonth -= 1;
            }
            strDay = Math.min(strDay,daysInMonth[strMonth]);//三、前一个月日期不一定和今天同一号,例如3.31的前一个月日期是2.28;9.30前一个月日期是8.30
        
            if(strMonth<10)//给个位数的月、日补零
            {
                strMonth="0"+strMonth;
            }
            if(strDay<10)
            {
                strDay="0"+strDay;
            }
            var datastr = strYear+"-"+strMonth+"-"+strDay;
        },
计算当前日期前一个月天数数组:

monthDayDate = (date) => {
let year = new Date(date).getFullYear() // 获取年份
let month = new Date(date).getMonth() + 1 // 获取月份
let allDay = new Date(year, month, 0).getDate() // 获取当月有多少天
let dateArr = []
// 循环天数push进数组
for (let i = 1; i <= allDay; i++) {
i = i < 10 ? '0' + i : i
dateArr.push(String(i))
}
console.log(dateArr) 
}

posted on 2021-11-11 11:26  Nymph_Zhu  阅读(667)  评论(0)    收藏  举报