当前位置:网站首页>JS to get the date in the next seven days of the current date

JS to get the date in the next seven days of the current date

2022-06-13 08:33:00 BUG_ Jia

getday2() {
      		let days = [];
      		var day = new Date();
      		for(let i=0; i<=144;i+=24){		//144 Is the number of hours in the first six days 
      			let dateItem=new Date(day.getTime() - i * 60 * 60 * 1000);	// Use the timestamp of the day minus the previous time in milliseconds ( Hours * branch * second * millisecond )
      			let y = dateItem.getFullYear();	// Get year 
      			let m = dateItem.getMonth() + 1;	// Get month js Month from 0 Start , need +1
      			let d= dateItem.getDate() + 7;	// Get date 
      			m = this.addDate0(m);	// Make up zero for odd months 
      			d = this.addDate0(d);	// Fill in zeros for odd dates 
      			let valueItem= y + '-' + m + '-' + d;	// Combine 
      			days.push(valueItem);	// Add to array 
      		}
      		console.log(' Date of last seven days :',days);
// Arrange the acquired dates in order 
          let arr=days.reverse();
          let obj={};
          for (let key in arr) {
               obj[key] = arr[key];
           };
          let newObj = Object.keys(obj).map(val => ({
                  day: obj[val]
           }));
          console.log(newObj)
      	},

      	// Add to the date 0
      	addDate0(time) {
      		if (time.toString().length == 1) {
      			time = '0' + time.toString();
      		}
      		return time;
      	},

原网站

版权声明
本文为[BUG_ Jia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270541410589.html