Skip to content

Instantly share code, notes, and snippets.

@mpr0xy
Created August 7, 2024 03:15
Show Gist options
  • Select an option

  • Save mpr0xy/d4fbb50eb7fc572471fce0f8ed8fbe80 to your computer and use it in GitHub Desktop.

Select an option

Save mpr0xy/d4fbb50eb7fc572471fce0f8ed8fbe80 to your computer and use it in GitHub Desktop.

Revisions

  1. mpr0xy created this gist Aug 7, 2024.
    22 changes: 22 additions & 0 deletions getDaysInWeek.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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;
    }