Created
August 7, 2024 03:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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