How do I calculate number of given weekday between range using Moment JS?
Asked Answered
A

4

5

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?

Antin answered 17/3, 2015 at 3:34 Comment(0)
H
13

I came up with the following function that seems to work for the given handful of test cases I tried:

function weekdaysBetween(d1, d2, isoWeekday) {
    // ensure we have valid moment instances
    d1 = moment(d1);
    d2 = moment(d2);
    // figure out how many days to advance to get to the next
    // specified weekday (might be 0 if d1 is already the 
    // specified weekday).
    var daysToAdd = ((7 + isoWeekday) - d1.isoWeekday()) % 7;
    var nextTuesday = d1.clone().add(daysToAdd, 'days');
    // if we are already passed the end date, there must not
    // be any of that day in the given period.
    if (nextTuesday.isAfter(d2)) {
        return 0;
    }
    // otherwise, just return the whole number of weeks
    // difference plus one for the day we already advanced to
    var weeksBetween = d2.diff(nextTuesday, 'weeks');
    return weeksBetween + 1;
}

You pass in the isoWeekday value for the day you are trying to count. e.g. for Tuesday, pass in 2.

Sample invocation:

var d1 = moment('2015-03-01');
var d2 = moment('2015-03-25');

console.log('result:', weekdaysBetween(d1, d2, 2)); // => result: 4

Wolfram Alpha gives the same result.

You should add your own tests before trusting this completely, though.

Hill answered 17/3, 2015 at 4:5 Comment(2)
I tweaked and extended this slightly to be inclusive (e.g. range from Monday, Jan 01, 2018 to the same Monday, would return 1 for Mondays). I also wrote some basic tests (for Jest / Jasmine) - in case that is useful for someone, see this gist.Regulus
This worked for me. For me I wanted to simply count sundays, then saturdays and it worked as expected.Resistencia
D
3

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
Donetta answered 17/3, 2015 at 4:5 Comment(1)
This is the vanilla JS version of the code I posted, which used the equivalent MomentJS methods. Good to get the verification my algorithm was correct.Hill
S
3

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)
Subaquatic answered 6/8, 2015 at 21:56 Comment(1)
Need to get the dates list :-)Subito
U
0

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');
}
Utopianism answered 28/1, 2016 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.