Get one week back from today's time

In uni-app, JavaScript Date objects and arrays can be used to intercept the seven-day number adjacent to today (counting the current number).
The following code will return two arrays, one array contains the seven days of the week from today (including today), and the other array contains the numbers corresponding to these seven days (including today),

// 获取当前时间的年月日
			const today = new Date();
			const dateArray = [];
			const dayOfWeekArray = [];
			const yearMonthDayArray = [];

			for (let i = 0; i < 7; i++) {
				const d = new Date(today);
				d.setDate(today.getDate() + i);

				dateArray.push(d.getDate());

				const dayOfWeekStr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][d.getDay()];
				dayOfWeekArray.push(dayOfWeekStr);

				const yearMonthDayStr = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
				yearMonthDayArray.push(yearMonthDayStr);
			}
			var num = [];

			for (let i = 0; i < dateArray.length; i++) {
				num.push({
					data: dateArray[i],
					numa: dayOfWeekArray[i],
					time: yearMonthDayArray[i]
				})
			}
			that.nums = num;
			console.log(that.nums);

The that here is to assign this to that variable.

Guess you like

Origin blog.csdn.net/m0_64590669/article/details/130539509