The shortest js code to implement "calculate the first seven days of the current date (accurate across months and years)"

The shortest js code to implement "calculate the first seven days of the current date (accurate across months and years)"

// 获取最近七天日期
    getDate() {
    
    
      let days = [];
      let day = new Date();
      for(let i = 0; i <= 144; i += 24){
    
    		//144是前六天的小时数
        let dateItem=new Date(day.getTime() - i * 60 * 60 * 1000);	//使用当天时间戳减去以前的时间毫秒(小时*分*秒*毫秒)

        let y = dateItem.getFullYear();	//获取年份
        let m = (dateItem.getMonth() + 1).toString().padStart(2, '0');	//获取月份并补零,月份从0开始,需要+1
        let d= dateItem.getDate().toString().padStart(2, '0');	//获取日期并补零

        days.push(y + '-' + m + '-' + d);
      }
      console.log("最近七天日期", days)
    },

Guess you like

Origin blog.csdn.net/TurtleOrange/article/details/125329340