0

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);
Luuk S
  • 13
  • 6

2 Answers2

0

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;
}
Alessandro
  • 165
  • 10
0

Here's how I would do it :

function daysWorked(firstDay, lastDay) {
  let daysWorked = 0;

  for (
    let cursor = new Date(+firstDay);
    cursor.getTime() <= lastDay.getTime();
    cursor.setDate(cursor.getDate() + 1)
  ) {
    let day = cursor.getDay();
    // skip Saturdays and Sundays
    if (day === 0 || day === 6) {
      continue;
    }
    daysWorked++;
  }
  return daysWorked;
}

I have the feeling this might be a little less efficient than the previous answer depending how good is JavaScript working with dates.

Let me try a little benchmark...

From 2019-06-16 to 2019-09-02

  • 1 million times dateDifference() from previous answer : 3698 miliseconds
  • 1 million times daysWorked() from my answer : 47979 miliseconds

12 times slower...

From 2015-06-16 to 2019-09-02

  • 10k times dateDifference() from previous answer : 95 miliseconds
  • 10k times daysWorked() from my answer : 9054 miliseconds

~100 times slower...

Okay forget my answer xD

Mouradif
  • 2,666
  • 1
  • 20
  • 37