-1

How do I get every weekday (monday through friday) in a week from a given week number

input

var weekNumber = "week + " " + year"; //"35 2020"

Wanted output

var weekArray = ["2020-08-24", "2020-08-25", "2020-08-26", "2020-08-27", "2020-08-28"];
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Does this answer your question? [javascript calculate date from week number](https://stackoverflow.com/questions/16590500/javascript-calculate-date-from-week-number) – Justinas Aug 27 '20 at 11:11
  • @Justinas not completely it returns the first and last date in a week. + i am stupid and dont know what to do if the week starts in one month and ends in another – Kristoffer Clausen Aug 27 '20 at 11:17
  • You have start date, then you use momentJs to loop 7 times and in each iteration add date to `weekArray` – Justinas Aug 27 '20 at 11:19
  • The purpose of this site is not to write code for you, it's to help you with code you've written. The [answer linked](https://stackoverflow.com/questions/16590500/javascript-calculate-date-from-week-number) by @Justinas is all you need to get started, after that it's just a matter of looping over the days of the week and formatting the output. Those topics are also covered here in multiple previous questions. – RobG Aug 27 '20 at 22:17

1 Answers1

4

Here's an example building on top of: javascript calculate date from week number

Get the first date of the week, then return an array of 7 elements, incrementing the date for each element.

If the day number goes above the number of days in the month, then add one to the month temp.m and reset the day temp.d equal to one.

function getISOWeek(w, y) {
    var simple = new Date(y, 0, 1 + (w - 1) * 7);
    var dow = simple.getDay();
    var ISOweekStart = simple;
    if (dow <= 4)
        ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
    else
        ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
    const temp = {
      d: ISOweekStart.getDate(),
      m: ISOweekStart.getMonth(),
      y: ISOweekStart.getFullYear(),
    }
    //console.log(ISOweekStart)
    const numDaysInMonth = new Date(temp.y, temp.m + 1, 0).getDate()
    
    return Array.from({length: 7}, _ => {
      if (temp.d > numDaysInMonth){
        temp.m +=1;
        temp.d = 1;
        // not needed, Date(2020, 12, 1) == Date(2021, 0, 1)
        /*if (temp.m >= 12){
          temp.m = 0
          temp.y +=1
        }*/
      }      
      return new Date(temp.y, temp.m, temp.d++).toUTCString()
    });
}

// var weekNumber = "week + " " + year"; //"35 2020"
const weekNumber = "53 2020";

const weekYearArr = weekNumber.split(" ").map(n => parseInt(n))

const weekOut = getISOWeek(...weekYearArr) 

console.log(weekOut);

Input: "35 2020"

Output:

[
  "Sun, 23 Aug 2020 22:00:00 GMT",
  "Mon, 24 Aug 2020 22:00:00 GMT",
  "Tue, 25 Aug 2020 22:00:00 GMT",
  "Wed, 26 Aug 2020 22:00:00 GMT",
  "Thu, 27 Aug 2020 22:00:00 GMT",
  "Fri, 28 Aug 2020 22:00:00 GMT",
  "Sat, 29 Aug 2020 22:00:00 GMT"
]

input : "36 2020"

output:

[
  "Sun, 30 Aug 2020 22:00:00 GMT",
  "Mon, 31 Aug 2020 22:00:00 GMT",
  "Tue, 01 Sep 2020 22:00:00 GMT",
  "Wed, 02 Sep 2020 22:00:00 GMT",
  "Thu, 03 Sep 2020 22:00:00 GMT",
  "Fri, 04 Sep 2020 22:00:00 GMT",
  "Sat, 05 Sep 2020 22:00:00 GMT"
]
Alex L
  • 4,168
  • 1
  • 9
  • 24