Skip to content

Instantly share code, notes, and snippets.

@mpr0xy
Created August 7, 2024 03:15
Show Gist options
  • Save mpr0xy/d4fbb50eb7fc572471fce0f8ed8fbe80 to your computer and use it in GitHub Desktop.
Save mpr0xy/d4fbb50eb7fc572471fce0f8ed8fbe80 to your computer and use it in GitHub Desktop.
Get the date data for a certain month and organize it by week
function getDaysInWeek(year, month) {
function getDaysInMonth(year, month) {
return new Date(year, month, 0).getDate();
}
const monthDays = getDaysInMonth(year, month);
console.log(monthDays);
let startDate = 1;
const results = [];
while (startDate <= monthDays) {
const week = [0, 0, 0, 0, 0, 0, 0];
let day = new Date(year, month - 1, startDate).getDay();
day = day === 0 ? 6 : day - 1;
week.forEach((item, index) => {
if (index >= day && startDate <= monthDays) {
week[index] = startDate;
startDate++;
}
});
results.push(week);
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment