Calculate the number of specific business days between dates

I am trying to calculate the number of Mondays, Wednesdays and Fridays between two dates in Tasker, so I need a mathematical formula, possibly using a date in the form of seconds, i.e. unix time or javascript code. I tried Googling and broke my brain in any way to even start it, and I got lost, so I have not tried anything yet. The only thing I could think of was to get the total number of days and divide by 7, but this obviously doesn’t really help me, especially if one or both days is in the middle of the week. Can someone point me in a better direction?

0
source share
3 answers

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);
}

, [1,3,5] :

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

, Date 0, 1 2014 Date(2014,8,1).

+5

, n. n / 7. n = n % 7. n .

:

, Friday, 100. , n = 100. 100/7 14 . 100 % 7 = 2. , ,

Friday -> 14+1 = 15
Saturday -> 14+1 = 15
Sunday -> 14
Monday -> 14
Tuesday -> 14
Wednesday -> 14
Thursday -> 14
+1

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;
    }
}
-1

Source: https://habr.com/ru/post/1606064/


All Articles