Home > Mobile >  How to iterate week number, start/end date of weeks using moment
How to iterate week number, start/end date of weeks using moment

Time:04-05

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>

CodePudding user response:

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) ))
  • Related