Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
How to iterate week number, start/end date of weeks using moment

How can I get all week number, from/to date of specific month?

The below code is what I construct to display week number, from/to date of specific month and it returns infinite loop whenever I insert the function inside loop.

function getISOWeeksInMonth(month, year) {
    let weekStart = new Date(year, month - 1, 1);

    weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);

    let weekEnd = new Date(weekStart);
    weekEnd.setDate(weekEnd.getDate() + 6);

    let weekNum = moment(weekStart, "YYYY-MM-DD").week()

    let weeks = [];

    do {
        weeks.push({
            weekNum : weekNum++,
            start: new Date(weekStart),
            end: new Date(weekEnd)
        });

        weekStart.setDate(weekStart.getDate() + 7);
        weekEnd.setDate(weekEnd.getDate() + 7);
    } while (weekStart.getMonth() < month);

    return weeks;
}

_.forEach(moment.months(), function (month_name) {
    var month_number = moment().month(month_name).format("MM");

    getISOWeeksInMonth(month_number, 2022).forEach(week => console.log(
       'Week : ' + week.weekNum +
       '\nStart: ' + week.start.toDateString() + 
       '\nEnd  : ' + week.end.toDateString())
    );
})

Result should return the list the week number, from/to date
Sample Url: https://savvytime.com/week-number/philippines/2022

Here is the sample when I run the function outside loop:

function getISOWeeksInMonth(month, year) {
    let weekStart = new Date(year, month - 1, 1);

    weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);

    let weekEnd = new Date(weekStart);
    weekEnd.setDate(weekEnd.getDate() + 6);


    let weekNum = moment(weekStart, "YYYY-MM-DD").week()

    let weeks = [];

    do {
        weeks.push({
            weekNum : weekNum++,
            start: new Date(weekStart),
            end: new Date(weekEnd)
        });

        weekStart.setDate(weekStart.getDate() + 7);
        weekEnd.setDate(weekEnd.getDate() + 7);
    } while (weekStart.getMonth() < month);

    return weeks;
}

getISOWeeksInMonth(1, 2022).forEach(week => console.log(
   'Week : ' + week.weekNum +
   '\nStart: ' + week.start.toDateString() + 
   '\nEnd  : ' + week.end.toDateString())
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function getISOWeeksInMonth(month, year) {
    let weekStart = new Date(year, month - 1, 1);

    weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);

    let weekEnd = new Date(weekStart);
    weekEnd.setDate(weekEnd.getDate() + 6);

    let weeks = [];

    do {
        let weekNum = moment(weekStart, "YYYY-MM-DD").week()

        weeks.push({
            weekNum : weekNum,
            start: new Date(weekStart),
            end: new Date(weekEnd)
        });

        weekStart.setDate(weekStart.getDate() + 7);
        weekEnd.setDate(weekEnd.getDate() + 7);
    } while (weekStart.getMonth() < month && (weekStart.getMonth() || (month < 12) ));

    return weeks;
}

let _ = moment.months()
_.forEach(function (month_name) {
    var month_number = moment().month(month_name).format("MM");

    getISOWeeksInMonth(month_number, 2022).forEach(week => console.log(
       'Week : ' + week.weekNum +
       '\nStart: ' + week.start.toDateString() + 
       '\nEnd  : ' + week.end.toDateString())
    );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>

When you calculate the weeks in December (month 12), you're starting with the last week in the prior month (November, i.e. 11), and in your do {...} while loop, you're adding 7 days to weekStart. So far so good, but your while (weekStart.getMonth() < month) is checking the 0-11 value of weekStart.getMonth(), and when it rolls forward from 11, instead of going to 12 it goes to 0. So it is always less than 12 (December!)!

You can fix this by adjusting your while to:

while (weekStart.getMonth() < month && (weekStart.getMonth() || (month < 12) ))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!