javascriptdatedaysweekend

I have trouble with taking away the weekend in my code


I'm making a site page for school where a person can put in two dates:

  1. day started working for company
  2. day stopped working for company (when the contract ends)

these days are filled in via the format MM-DD-YYYY

When a person fills in a start date it'll calculate the following formula: "days worked = todays date - the date that the person has started working" after that it'll calculate it into days instead of miliseconds (days worked/1000/60/60/24).

Now i have to get rid of the Saturday and Sunday of every week that one person has worked.

Edit: It has been Fixed, Thanks all

Javascript Code

    function days_of_a_year(year) { return isLeapYear(year) ? 366 : 365; }
    function isLeapYear(year) { return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0); }
    var year    = moment().year();
    var days_year = days_of_a_year(moment().year());
    $(document).ready(function(){


    $(".form-control").keyup(function(){
        //get
        var leave_days = $('#leave_days').val(),
         leave_hours = $('#leave_hours').val(),
         hours_employee_week = $('#hours_employee_week').val(),
         hours_week = $('#hours_week').val(),
         date_employed = $('#date_employed').val(),
         date_unemployed = $('#date_unemployed').val(),

         start = new Date(date_employed),
         end   = new Date(date_unemployed),
         diff  = new Date(end - start),
         days  = Math.round(diff/1000/60/60/24),
            now = new Date(),
            days_worked = new Date(now - start),
         year = moment().year();


        var leave_hours_full = leave_days * leave_hours;
        var perc_employment =  hours_employee_week / hours_week * 100;
        var leave_hours_year = leave_hours_full * (hours_employee_week / hours_week);

        var days_worked_year = Math.round(days_worked/1000/60/60/24);

        console.log(parseInt(days_worked_year));


        $('#days_worked_year').val(days);
        $('#days_full_year').val(days_year);
        $('#perc_worked_year').val(days_worked_year);

Solution

  • I believe this question is similar to yours. The accepted answer in that question provides this function:

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