exclude weekends in javascript date calculation

前端 未结 4 570
甜味超标
甜味超标 2020-11-28 16:22

I have two sets of codes that work. Needed help combining them into one.

This code gets me the difference between two dates. works perfectly:

functio         


        
相关标签:
4条回答
  • 2020-11-28 17:03

    When determining the number of days between two dates, there are lots of decisions to be made about what is a day. For example, the period 1 Feb to 2 Feb is generally one day, so 1 Feb to 1 Feb is zero days.

    When adding the complexity of counting only business days, things get a lot tougher. E.g. Monday 2 Feb 2015 to Friday 6 February is 4 elapsed days (Monday to Tuesday is 1, Monday to Wednesday is 2, etc.), however the expression "Monday to Friday" is generally viewed as 5 business days and the duration Mon 2 Feb to Sat 7 Feb should also be 4 business days, but Sunday to Saturday should be 5.

    So here's my algorithm:

    1. Get the total number of whole days between the two dates
    2. Divide by 7 to get the number of whole weeks
    3. Multiply the number of weeks by two to get the number of weekend days
    4. Subtract the number of weekend days from the whole to get business days
    5. If the number of total days is not an even number of weeks, add the numbe of weeks * 7 to the start date to get a temp date
    6. While the temp date is less than the end date:
      • if the temp date is not a Saturday or Sunday, add one the business days
      • add one to the temp date
    7. That's it.

    The stepping part at the end can probably be replaced by some other algorithm, but it will never loop for more than 6 days so it's a simple and reasonably efficient solution to the issue of uneven weeks.

    Some consequences of the above:

    1. Monday to Friday is 4 business days
    2. Any day to the same day in a different week is an even number of weeks and therefore an even mutiple of 5, e.g. Monday 2 Feb to Monday 9 Feb and Sunday 1 Feb to Sunday 8 Feb are 5 business days
    3. Friday 6 Feb to Sunday 7 Feb is zero business days
    4. Friday 6 Feb to Monday 9 Feb is one business day
    5. Sunday 8 Feb to: Sunday 15 Feb, Sat 14 Feb and Fri 13 Feb are all 5 business days

    Here's the code:

    // Expects start date to be before end date
    // start and end are Date objects
    function dateDifference(start, end) {
    
      // Copy date objects so don't modify originals
      var s = new Date(+start);
      var e = new Date(+end);
    
      // Set time to midday to avoid dalight saving and browser quirks
      s.setHours(12,0,0,0);
      e.setHours(12,0,0,0);
    
      // Get the difference in whole days
      var totalDays = Math.round((e - s) / 8.64e7);
    
      // Get the difference in whole weeks
      var wholeWeeks = totalDays / 7 | 0;
    
      // Estimate business days as number of whole weeks * 5
      var days = wholeWeeks * 5;
    
      // If not even number of weeks, calc remaining weekend days
      if (totalDays % 7) {
        s.setDate(s.getDate() + wholeWeeks * 7);
    
        while (s < e) {
          s.setDate(s.getDate() + 1);
    
          // If day isn't a Sunday or Saturday, add to business days
          if (s.getDay() != 0 && s.getDay() != 6) {
            ++days;
          }
        }
      }
      return days;
    }
    

    Dunno how it compares to jfriend00's answer or the code you referenced, if you want the period to be inclusive, just add one if the start or end date are a business day.

    0 讨论(0)
  • 2020-11-28 17:13

    @RobG has given an excellent algorithm to separate business days from weekends. I think the only problem is if the starting days is a weekend, Saturday or Sunday, then the no of working days/weekends will one less.

    Corrected code is below.

    function dateDifference(start, end) {
    
      // Copy date objects so don't modify originals
    
      var s = new Date(start);
      var e = new Date(end);
    
        var addOneMoreDay = 0;
        if( s.getDay() == 0 || s.getDay() == 6 ) {
        addOneMoreDay = 1;
      }
    
      // Set time to midday to avoid dalight saving and browser quirks
      s.setHours(12,0,0,0);
      e.setHours(12,0,0,0);
    
      // Get the difference in whole days
      var totalDays = Math.round((e - s) / 8.64e7);
    
      // Get the difference in whole weeks
      var wholeWeeks = totalDays / 7 | 0;
    
      // Estimate business days as number of whole weeks * 5
      var days = wholeWeeks * 5;
    
      // If not even number of weeks, calc remaining weekend days
      if (totalDays % 7) {
        s.setDate(s.getDate() + wholeWeeks * 7);
    
        while (s < e) {
          s.setDate(s.getDate() + 1);
    
          // If day isn't a Sunday or Saturday, add to business days
          if (s.getDay() != 0 && s.getDay() != 6) {
            ++days;
          }
          //s.setDate(s.getDate() + 1);
        }
      }
      var weekEndDays = totalDays - days + addOneMoreDay;
      return weekEndDays;
    }
    

    JSFiddle link is https://jsfiddle.net/ykxj4k09/2/

    0 讨论(0)
  • 2020-11-28 17:14

    First Get the Number of Days in a month

         totalDays(month, year) {
            return new Date(year, month, 0).getDate();
         }
    

    Then Get No Of Working Days In A Month By removing Saturday and Sunday

      totalWorkdays() {
         var d = new Date(); // to know present date
         var m = d.getMonth() + 1; // to know present month
         var y = d.getFullYear(); // to knoow present year
         var td = this.totalDays(m, y);// to get no of days in a month
         for (var i = 1; i <= td; i++) {  
            var s = new Date(y, m - 1, i);
            if (s.getDay() != 0 && s.getDay() != 6) {
               this.workDays.push(s.getDate());// working days
            }else {
               this.totalWeekDays.push(s.getDate());//week days
             }
          }
          this.totalWorkingDays = this.workDays.length;
     }
    
    0 讨论(0)
  • 2020-11-28 17:19

    Here's a simple function to calculate the number of business days between two date objects. As designed, it does not count the start day, but does count the end day so if you give it a date on a Tuesday of one week and a Tuesday of the next week, it will return 5 business days. This does not account for holidays, but does work properly across daylight savings changes.

    function calcBusinessDays(start, end) {
        // This makes no effort to account for holidays
        // Counts end day, does not count start day
    
        // make copies we can normalize without changing passed in objects    
        var start = new Date(start);
        var end = new Date(end);
    
        // initial total
        var totalBusinessDays = 0;
    
        // normalize both start and end to beginning of the day
        start.setHours(0,0,0,0);
        end.setHours(0,0,0,0);
    
        var current = new Date(start);
        current.setDate(current.getDate() + 1);
        var day;
        // loop through each day, checking
        while (current <= end) {
            day = current.getDay();
            if (day >= 1 && day <= 5) {
                ++totalBusinessDays;
            }
            current.setDate(current.getDate() + 1);
        }
        return totalBusinessDays;
    }
    

    And, the jQuery + jQueryUI code for a demo:

    // make both input fields into date pickers
    $("#startDate, #endDate").datepicker();
    
    // process click to calculate the difference between the two days
    $("#calc").click(function(e) {
        var diff = calcBusinessDays(
            $("#startDate").datepicker("getDate"), 
            $("#endDate").datepicker("getDate")
        );
        $("#diff").html(diff);
    });
    

    And, here's a simple demo built with the date picker in jQueryUI: http://jsfiddle.net/jfriend00/z1txs10d/

    0 讨论(0)
提交回复
热议问题