'How do I calculate number of given weekday between range using Moment JS?

How do I find out how many of a given weekday (ex: Tuesdays) are in a date range using Moment JS and jQuery?

I can find the number of days using Difference: http://momentjs.com/docs/#/displaying/difference/

And I know the start and end date, so I feel like it should be possible?

Example: How many Tuesdays are there between March 1st and March 25th?



Solution 1:[1]

If you're trying to find the number of weekdays W (e.g. Monday, Tuesday, …) between the two dates N and M, you can:

  1. Find the next occurance N' of W after N.
  2. Find the number of days between N' and M.
  3. If N' is after M, there are no W's between. Otherwise, the number of W days should then be 1 + floor((M-N)/7).
function getWeekdaysBetweenDates(firstDate, secondDate, dayOfWeek) {
    var MILISECONDS_IN_DAY = 86400000;

    function getNextDayOfWeek(date, dayOfWeek) {
        date.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
        return date;
    }

    firstDate = getNextDayOfWeek(firstDate, dayOfWeek);
    if (firstDate > secondDate) {
        return 0;
    }

    return 1 + Math.floor(((secondDate - firstDate) / MILISECONDS_IN_DAY) / 7);
}

var firstDate = new Date("March 1, 2015");
var secondDate = new Date("March 25, 2015");
console.log(getWeekdaysBetweenDates(firstDate, secondDate, 2));
// 4

Solution 2:[2]

https://github.com/andruhon/moment-weekday-calc does what you need. It calculates specific weekdays in the range, with option to exclude particular days (say public holidays)

Usage:

moment().isoWeekdayCalc({  
  rangeStart: '1 Apr 2015',  
  rangeEnd: '31 Mar 2016',  
  weekdays: [1,2,3,4,5], //weekdays Mon to Fri
  exclusions: ['6 Apr 2015','7 Apr 2015']  //public holidays
}) //returns 260 (260 workdays excluding two public holidays)

Solution 3:[3]

let dayno = [0, 1, 2, 3, 4, 5, 6]
for (let i = 0; i < dayno.length; i++) {
      let start = moment('2022-1-01')
      let end = moment('2022-1-31')
      getweek(start, end, dayno[i])
    }

var dataArray = []

  const getweek = (start, end, dayno) => {
    var arr = []
    let tmp = start.clone().day(dayno)
    if (tmp.isAfter(start, 'd')) {
      arr.push(tmp.format('YYYY-MM-DD'))
    }
    while (tmp.isBefore(end)) {
      tmp.add(7, 'days')
      if (tmp.format('M') == start.format('M')) {
        arr.push(tmp.format('YYYY-MM-DD'))
      }
    }

    const dow = moment(start).startOf('month').day()

    if (dow + 1 == dayno + 1) {
      var obj = {
        name: dayno + 1,
        value: arr.length + 1,
      }
    } else {
      var obj = {
        name: dayno + 1,
        value: arr.length,
      }
    }

    dataArray.push(obj)
  }

Solution 4:[4]

if you are like me with a mind that get confused on maths:

var startDate = moment().startOf('month');
var endDate = moment().endOf('month');

var aDate = moment(startDate).day('Sunday');

var weekDays = 0;
while (aDate.isSameOrBefore(endDate)) {
   if (aDate.isSameOrAfter(startDate) && aDate.isSameOrBefore(endDate))
        weekDays++;
   aWeekDayDate.add(1, 'week');
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kba
Solution 2 Andrew Kondratev
Solution 3 Ayush Gupta
Solution 4