Catch only days of the month by removing Saturday and Sunday with JAVASCRIPT/TYPESCRIPT

Asked

Viewed 647 times

0

How can I add only the days of the month into an array by removing Saturday and Sunday?

user will inform the month and the year and with this month I pick up the days.

ex:

March 2018

1 2 5 6 7 8 9 12 13 14 15 16 19 20 21 22 23 26 27 28 29 30

  • I suggest you take a look at Moment.js

2 answers

1

Without the Moment-js, why... put an entire lib just for this until the C:

function getWeekDaysInMonth(month = 0, year = 2018) {
  console.log('Weekdays for', new Date(year, month, 1).toString())
  return new Array(new Date(year, month+1, 0).getDate()).fill().map((n, i) => {
    const weekDay = new Date(year, month, ++i).getDay();
    return weekDay > 0 && weekDay < 6 && i;
  }).filter(val => !!val);
}

console.log(getWeekDaysInMonth(2, 2018))

Outputs: [ 1, 2, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 26, 27, 28, 29, 30 ]

Repl.it Session

Explaining:

First we’ll have to iterate through the total days of the month that is provided, in this case Marco (0 based, then 2) - this loop is done by constructing an array of length equal to the maximum days of the month that is then filled with "nothing" (via fill() and iterated by map().

then we have to see if the day of the week of that date is greater than 0 (Sunday) or 6 (Saturday) and if yes, return the day of the month.

once this mapping is over, we will filter from the array all values that are not positive.

:)

0

Recently I had to recover all the Sundays, as Farms and the Saturdays of the current month to generate a scale. I found the plugin Moment-weekdaysin to the library Moment js..

You must perform the moment passing as parameter the year and month that the user informs: moment('2018-03')

Then you must execute the method weekdaysInMonth of the plugin Moment-weekdaysin, informing a array with values of 0 to 6.

0 = Domingo
1 = Segunda
2 = Terça
3 = Quarta
4 = Quinta
5 = Sexta
6 = Sábado

Like you just want to Second à Sixth had turned out like this: weekdaysInMonth([1,2,3,4,5])

Working example.

// Define o idioma
moment.locale('pt-br');
let btn = document.querySelector('#btn');
let mes = document.querySelector('#mes');
let ano = document.querySelector('#ano');
const getDaysOfMonth = () => {
  const datas = moment(`${ano.value}-${mes.value}`).weekdaysInMonth([1,2,3,4,5]);
  datas.forEach(data => console.log(data.format('L')));
};
btn.addEventListener('click', getDaysOfMonth);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment-with-locales.js"></script>
<script src="https://cdn.rawgit.com/kodie/moment-weekdaysin/master/moment-weekdaysin.js"></script>

<input type="number" id="mes" placeholder="Mês" min="1" max="12" value="3">
<input type="number" id="ano" placeholder="Ano" min="1990" max="2050" value="2018">
<button id="btn">OK</button>

Browser other questions tagged

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