Ad
Dates/Time
Data Types

Description:
You are given an array of dates. Your task is to create an array of arrays where each sub array is all the dates contained in that week. A week starts on Monday, and ends on Sunday.

Example Input:
[
"3/24/2021",
"3/25/2021",
"3/1/2021",
"3/6/2021",
"3/10/2021"
]

Expected Output
[
["3/1/2021", "3/6/2021"],
["3/10/2021"],
["3/24/2021", "3/25/2021"],
]

Code
Diff
  • function groupByWeek(days) {
      const dates = days.map(a => new Date(a)).sort((a, b) => b - a);
    
      let currentMonday = new Date();
      currentMonday.setDate(dates[0].getDate() - dates[0].getDay())
      
      let currentSunday = new Date()
      currentSunday.setDate(currentMonday.getDate() + 6);
      
      let weeks = []
      let currentWeek = []
      for(let date of dates) {
        if(date >= currentMonday && date <= currentSunday) {
          currentWeek.push((date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear())
        } else {
          if(currentWeek.length > 0) {
            weeks.push(currentWeek)
          }
          currentWeek = [(date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear()]
          currentMonday.setDate(date.getDate() - date.getDay())
          currentSunday.setDate(currentMonday.getDate() + 6);
        }
        
      }
      if(currentWeek.length > 0) {
        weeks.push(currentWeek)
      }
      
      return weeks
    }
    • function groupByWeek(days) {
    • var dates = days.map(a => new Date(a)).sort((a, b) => b - a);
    • console.log(dates.map(a => (a.getMonth()+1) + "/" + a.getDate() + "/" + a.getFullYear()))
    • const dates = days.map(a => new Date(a)).sort((a, b) => b - a);
    • var currentMonday = new Date();
    • let currentMonday = new Date();
    • currentMonday.setDate(dates[0].getDate() - dates[0].getDay())
    • var currentSunday = new Date()
    • let currentSunday = new Date()
    • currentSunday.setDate(currentMonday.getDate() + 6);
    • //console.log(firstMonday.getDay())
    • //console.log(firstSunday.getDay())
    • var weeks = []
    • var currentWeek = []
    • for(var date of dates) {
    • let weeks = []
    • let currentWeek = []
    • for(let date of dates) {
    • if(date >= currentMonday && date <= currentSunday) {
    • currentWeek.push((date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear())
    • } else {
    • if(currentWeek.length > 0) {
    • weeks.push(currentWeek)
    • }
    • currentWeek = [(date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear()]
    • currentMonday.setDate(date.getDate() - date.getDay())
    • currentSunday.setDate(currentMonday.getDate() + 6);
    • }
    • }
    • if(currentWeek.length > 0) {
    • weeks.push(currentWeek)
    • }
    • return weeks
    • }
Dates/Time
Data Types

You are given an array of dates. Your task is group the dates into seperate weeks. The week starts on Monday, and ends on Sunday.

example input

[
3/24/2021,
3/25/2021,
3/1/2021,
3/6/2021,
3/10/2021
]

expected output

[
[3/1/2021, 3/6/2021],
[3/10/2021],
[3/24/2021, 3/25/2021],
]

function groupByWeek(days) {
  var dates = days.map(a => new Date(a)).sort((a, b) => b - a);
  
  console.log(dates.map(a => (a.getMonth()+1) + "/" + a.getDate() + "/" + a.getFullYear()))

  var currentMonday = new Date();
  currentMonday.setDate(dates[0].getDate() - dates[0].getDay())
  
  var currentSunday = new Date()
  currentSunday.setDate(currentMonday.getDate() + 6);
  
  //console.log(firstMonday.getDay())
  //console.log(firstSunday.getDay())
  
  var weeks = []
  var currentWeek = []
  for(var date of dates) {
    if(date >= currentMonday && date <= currentSunday) {
      currentWeek.push((date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear())
    } else {
      if(currentWeek.length > 0) {
        weeks.push(currentWeek)
      }
      currentWeek = [(date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear()]
      currentMonday.setDate(date.getDate() - date.getDay())
      currentSunday.setDate(currentMonday.getDate() + 6);
    }
    
  }
  if(currentWeek.length > 0) {
    weeks.push(currentWeek)
  }
  
  return weeks
  
}