简体   繁体   中英

I need help creating a script to pull the current dates for the week

I'm not very familiar with JavaScript but I want to create a script that will pull the every single day of the week for me. Here is what I am working with right now. 在此处输入图片说明

I added the picture "Get date" and I would like to make it so that when I hit the "get date" button, it will go through the script and assign dates to the cells above Monday, Tuesday, Wednesday, etc. as 8/8, 8/9, 8/10.

I think you'll have to do it in two steps:

  1. Find the last Monday (which is up to 6 days in the past)
  2. Starting from there, build a list of 7 days

 var date = new Date(), daysOfWeek = []; // find last Monday while(date.getDay() != 1) { date.setDate(date.getDate() - 1); } // build array of dates for(var n = 0; n < 7; n++) { daysOfWeek.push((date.getMonth() + 1) + '/' + date.getDate()); date.setDate(date.getDate() + 1); } console.log(daysOfWeek); 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM