I have implemented this view (screenshot):
now it is current week by default. on next click, I want next week array and same for previous.
Below is the code for current week
this.weekDays = [];
var startweek = 0;
var curr = new Date; // get current date
for (let i = 0; i <= 6; i++) {
let first = curr.getDate() - curr.getDay() + i;
let day = new Date(curr.setDate(first)).toISOString().slice(0, 10)
this.weekDays.push(day)
}
console.log(this.weekDays)
So I got the array of dates:
[ "2020-07-26", "2020-07-27", "2020-07-28", "2020-07-29", "2020-07-30", "2020-07-31", "2020-08-01" ]
Now I need the same type of array of next week and previous week. How to get this array?
const dates = (startDate, num) => Array.from( { length: num }, (_, i) => new Date(startDate.getTime() + (i * 60000 * 60 * 24)).toISOString().slice(0, 10) ); const lastWeek = () => { let date = new Date(); date.setDate(date.getDate() - date.getDay() - 6); return dates(date, 7); } const nextWeek = () => { let date = new Date(); date.setDate(date.getDate() - date.getDay() + 8); return dates(date, 7); } console.log(lastWeek()); console.log(nextWeek());
function getWeekFromStartDay(start) { var weekDays = []; var curr = new Date(); // get current date var first = curr.getDate() - curr.getDay() + start; for (let i = first; i < first + 7; i++) { let day = new Date(curr.setDate(i)).toISOString().slice(0, 10); weekDays.push(day); } return weekDays; } console.log("Last week"); console.log(getWeekFromStartDay(-7)); console.log("This week"); console.log(getWeekFromStartDay(0)); console.log("Next week"); console.log(getWeekFromStartDay(7));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.