4

I'm trying to calculate the number of Mondays, Wednesdays, and Fridays between 2 dates in Tasker, thus I need a math formula, possibly utilizing the date in seconds form, i.e. unix time, or a javascript code. I've tried Googling and racking my brain for any way to even start this and I'm lost so I haven't tried anything yet. The only thing I could think of was getting the total number of days and dividing by 7, but that clearly does not help me very much, especially if one or both of the days is midweek. Can anyone point me in a better direction?

HeatherLeigh
  • 71
  • 2
  • 10

4 Answers4

11

How to count specific days of the week between two dates in O(1):

// days is an array of weekdays: 0 is Sunday, ..., 6 is Saturday
function countCertainDays( days, d0, d1 ) {
  var ndays = 1 + Math.round((d1-d0)/(24*3600*1000));
  var sum = function(a,b) {
    return a + Math.floor( ( ndays + (d0.getDay()+6-b) % 7 ) / 7 ); };
  return days.reduce(sum,0);
}

Example on counting Mondays, Wednesdays, and Fridays [1,3,5] between two dates:

countCertainDays([1,3,5],new Date(2014,8,1),new Date(2014,8,1)) // 1
countCertainDays([1,3,5],new Date(2014,8,1),new Date(2014,8,2)) // 1
countCertainDays([1,3,5],new Date(2014,8,1),new Date(2014,8,3)) // 2
countCertainDays([1,3,5],new Date(2014,8,1),new Date(2014,8,4)) // 2
countCertainDays([1,3,5],new Date(2014,8,1),new Date(2014,8,5)) // 3
countCertainDays([1,3,5],new Date(2014,8,1),new Date(2014,8,6)) // 3
countCertainDays([1,3,5],new Date(2014,8,1),new Date(2014,8,7)) // 3

Note that the month parameter to Date is 0-based, so 1 Sept 2014 is Date(2014,8,1).

Community
  • 1
  • 1
Matt
  • 20,108
  • 1
  • 57
  • 70
  • This is very useful. Is there any formula that i can give an array of dates too which will be exclude or will not count between two dates? – Poonam Jul 20 '16 at 12:16
  • @Poonam In that case just loop over the array of dates, and count the ones that are between the two dates. If your situation is more complex than that, please submit it as a separate question. – Matt Jul 20 '16 at 16:22
1

Assume total number of days is n. Then number of any day of a week is initially n / 7. Now take n = n % 7. Depending on the value of current n you can easily calculate the final count of days.

As example:

Assume your first day is Friday and total number of days is 100. So, n = 100. There are minimum 100/7 or 14 of each weekday is in the interval. 100 % 7 = 2. So final count is,

Friday -> 14+1 = 15
Saturday -> 14+1 = 15
Sunday -> 14
Monday -> 14
Tuesday -> 14
Wednesday -> 14
Thursday -> 14
taufique
  • 2,701
  • 1
  • 26
  • 40
  • @HeatherLeigh, If I could provide you the exact thing you were expecting, I think accepting the answer would not hurt you much, would it? – taufique Aug 29 '14 at 05:54
  • I need it automated (hence the use of Tasker) so it could take 2 dates and output the number of Mondays, Wednesdays, and Fridays between them. – HeatherLeigh Aug 29 '14 at 06:00
  • I have created an algorithm based on your logic.Please check the answer @taufique – Ashish Shrestha Jun 09 '22 at 06:38
1
const start_date = "2022-06-02";// standard date format YYYY-MM-DD
const end_date = "2022-06-18";

const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; // standard week days


const getNumberOfWeekDays = (day) => {
 // this will return number of week days between given start and end date

    const startDay = moment(start_date).format("ddd");

    const index = days.indexOf(startDay); // index of the start day

    const totalDays = moment(end_date).diff(moment(start_date), "days"); // 16

    const numberOfDays = Math.round(totalDays / 7); // 16/7 = 2; number of sessions by week

    const remainingDays = totalDays % 7; // 16 % 7 = 2

    const newArray = days.rotate(index).slice(0, remainingDays); // ["Thu","Fri"]

    if (newArray.includes(day)) {
      return numberOfDays + 1;
    }

    return numberOfDays;
};



 getNumberOfWeekDays('Thu');// Thursday between given start and end date is 3
 getNumberOfWeekDays('Mon');// Wednesday between given start and end date is 2
Ashish Shrestha
  • 138
  • 2
  • 10
-2

How to count the number of friday between two date in JavaScript

function myfunction() {
    var frist = document.getElementById("dt_VacStart").value
    var answer = 0;
    var second = document.getElementById("dt_VacEnd").value;
    if (frist != null && second != null) {
        var startDate = new Date(frist);
        var endDate = new Date(second);
        var totalfriday = 0;
        for (var i = startDate; i <= endDate;) {
            if (i.getDay() ==5) {
                totalfriday++;
            }
            i.setTime(i.getTime() + 1000 * 60 * 60 * 24);
        }
        document.getElementById('<%=Friday.ClientID%>').value = totalfriday;
    } else {
        totalfriday = 0;
        document.getElementById('<%=Friday.ClientID%>').value = totalfriday;
    }
}
Teepeemm
  • 4,331
  • 5
  • 35
  • 58