Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda