How many days a week do you have in a month?

Asked

Viewed 847 times

1

Hello, good night!

I did a lot of research on the Internet and found nothing concerning this doubt! I would like to get how many days of the week there are within a month using javascript! Example:

Quantos sábados existe no mês de atual? = 5
Quantas terças-feiras existes no mês atual? = 4

I would like a script where you inform me the amount of days that exists in the current month.

Thank you in advance...

  • Depends on the month.

  • Exactly! I need a script that tells me the number of days according to the current month.

  • Hello, William, suggestion, most mathematical cases will be much more efficient than loop/recursion, so in the case here I did with mathematical operation that got 90% faster than with loops: https://answall.com/a/466531/3635, for sporadic uses is indifferent, but if you are going to consult many months it may be very advantageous to test this proposal.

2 answers

8

Using a while adding up days will solve the problem:

 while (month === date.getMonth()) {
   if (date.getDay() === type) count++;
   date.setDate(date.getDate() + 1); // testar o próximo dia
 }

But the increment of date.getDate() + 1 will generate increments and on average 30 loops, most of which are unnecessary if the required rule is a specific day of the week just add up +7 inside the loop instead of +1 and check if it’s the same month after and then yes make the sum count++, the function a little optimized can look like this (as the test done at the end of the answer became almost 3 times faster):

function getDaysByType(type, month, year) {
  const date = new Date(year ? year : new Date().getFullYear(), month, 1, 0, 0, 0);

  var count = 0, increment = 1;

  while (month === date.getMonth()) {
    if (date.getDay() === type) {
        count++;
        increment = 7;
    }

    date.setDate(date.getDate() + increment); // testar o próximo dia
  }
  return count;
}

console.log('Quantidade de Sábados em Janeiro:', getDaysByType(0, 0));
console.log('Quantidade de Sábados em Fevereiro:', getDaysByType(0, 1));
console.log('Quantidade de Sábados em Março:', getDaysByType(0, 2));
console.log('Quantidade de Sábados em Abril:', getDaysByType(0, 3));
console.log('Quantidade de Sábados em Maio:', getDaysByType(0, 4));
console.log('Quantidade de Sábados em Junho:', getDaysByType(0, 5));
console.log('Quantidade de Sábados em Julho:', getDaysByType(0, 6));

This way it will be 4 to 11 loops instead of 28 to 31 loops, which made ~70% faster.


However I have another proposal, to use the BASIC MATHEMATICS, because if we know that a day of the week can appear between 4 and 5 times in a same month with that we have already obtained by a calculation, we will only need to know which the first day of the week of a month and which the last

Thus became ~90% faster:

Recalling that 0 = Saturday, 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday, 6 = Friday

Month 0 (January) to 11 (December)

function sumWeekDaysInMonth(weekday, month, year)
{
    if (!year) year = new Date().getFullYear();

    // Obtem o ultimo dia do mês
    var daysInMonth = new Date(year, month + 1, 0).getDate();

    // Obtem primeiro dia da semana do mês
    var firstWeekDay = new Date(year, month, 1).getDay();

    return Math.floor((daysInMonth + (weekday + firstWeekDay) % 7) / 7);
}

console.log('Quantidade de Sábados em Janeiro (ano atual):', sumWeekDaysInMonth(6, 0));
console.log('Quantidade de Sábados em Fevereiro (ano atual):', sumWeekDaysInMonth(6, 1));
console.log('Quantidade de Sábados em Março (ano atual):', sumWeekDaysInMonth(6, 2));

console.log('----');

console.log('Quantidade de Sábados em Janeiro 1988:', sumWeekDaysInMonth(6, 0, 1988));
console.log('Quantidade de Sábados em Fevereiro 1988:', sumWeekDaysInMonth(6, 1, 1988));
console.log('Quantidade de Sábados em Março 1988:', sumWeekDaysInMonth(6, 2, 1988));

To better understand, we use the % to get what is left in a division, then:

day of the week + first day of the week of the month / total days week (7)

In January it was (6 + 3) %7 = 2 (would be as 6+3=9 and 9/7=1.2, rounding is 2)

Then sum the number of days in the month and divide by 7, 4 was the remaining result of division:

(31 + 2) / 7 = 4,714285714285714

Then use Math.floor() to return the smallest integer that will be "4" for the month of January 2020

View the benchmark of all codes: https://jsbench.me/lwkdh223r9/1

resultado do benchmark

  • With while optimized managed to run almost 3 times faster

  • With "math" was the fastest performing almost 10 times faster

  • This question should not be closed , had two answers signal that was understood , was a valid question in my modest opinion.

4


I made a library to generate dates, number of week etc that I use in my projects, can be useful to you.

In this case it is simple and you can do it in Javascript. Note that this script is insensitive to holidays :), you have to add this logic by hand.

function getWorkingDays(month) {
  const date = new Date(2020, month, 1, 0, 0, 0);
  let workingdays = 0;
  while (month === date.getMonth()) {
    const currentDay = date.getDay();
    const isWeekend = (currentDay === 6) || (currentDay === 0);
    if (!isWeekend) workingdays++; // se não é fds aumentar o nr
    date.setDate(date.getDate() + 1); // testar o próximo dia
  }
  return workingdays;
}

// lembra-te que meses têm base 0
// ou seja: janeiro é o mes 0, fevereiro o mes 1, etc...

console.log(getWorkingDays(0));
console.log(getWorkingDays(1));

To know the number of specific days you can make an adaptation of the code:

function getDaysByType(month, type) {
  const date = new Date(2020, month, 1, 0, 0, 0);
  let count = 0;
  while (month === date.getMonth()) {
    if (date.getDay() === type) count++;
    date.setDate(date.getDate() + 1); // testar o próximo dia
  }
  return count;
}

// sábado tem o nr 6
// domingo tem o nr 0
// segunda tem o nr 1
// etc

console.log(getDaysByType(0, 0));
console.log(getDaysByType(0, 1));
console.log(getDaysByType(0, 2));
console.log(getDaysByType(0, 3));
console.log(getDaysByType(0, 4));
console.log(getDaysByType(0, 5));
console.log(getDaysByType(0, 6));

  • This is not what the author asked, he asked for specific days, You can do with loop or math, taking the first day of the month and which day of the week it represents and the last day of the month and pick which day of the week represents... Then it would be enough to calculate the weeks and take in these 4 weeks how many times appears the type of day of the past week in the argument of the function; For example how many Saturdays have in a month or how many Wednesdays has in a month... Personally I think that with mathematics you can be more performatic.

  • @Guilhermenascimento I just woke up and I may be slow :P but in question AP writes "I would like to get how many days of the week there is within a month." - that’s what I answered, with a script that generates 1 number. It’s not right?

  • Yes, but it’s "specific" days and it doesn’t matter if it’s a work day or a holiday, he wants something like this contarDiasNoMes('sabado', 'janeiro', 2020); = 4 or contarDiasNoMes(6, 1, 2020); = 4, as he indicated in the examples

  • @Guilhermenascimento I’m better now, thank you! I’ve added another variant.

  • 1

    I made a suggestion, many operations can be very good optimized with matematica https://answall.com/a/466531/3635

Browser other questions tagged

You are not signed in. Login or sign up in order to post.