0

I'm making home page for employee management. I want to get business hours between two dates. I already searched and get one method from this link. http://rion.io/2014/06/20/calculating-business-hours-in-javascript/

// Simple function that accepts two parameters and calculates
// the number of hours worked within that range
function workingHoursBetweenDates(startDate, endDate) {  
    // Store minutes worked
    var minutesWorked = 0;

    // Validate input
    if (endDate < startDate) { return 0; }

    // Loop from your Start to End dates (by hour)
    var current = startDate;

    // Define work range
    var workHoursStart = 9;
    var workHoursEnd = 18;
    var includeWeekends = false;

    // Loop while currentDate is less than end Date (by minutes)
    while(current <= endDate){          
        // Is the current time within a work day (and if it 
        // occurs on a weekend or not)          
        if(current.getHours() >= workHoursStart && current.getHours() < workHoursEnd && (includeWeekends ? current.getDay() !== 0 && current.getDay() !== 6 : true)){
              minutesWorked++;
        }

        // Increment current time
        current.setTime(current.getTime() + 1000 * 60);
    }

    // Return the number of hours
    return minutesWorked / 60;
}

I tried this method but performance is bad when date difference large such as 2020-01-01 12:45:00 - 2028-10-06 23:10:33.

So I make new function myself. But sometimes is wrong.

function workingHoursBetweenDates(startDate, endDate) {
  const startWeekday = startDate.getDay();
  const endWeekday = endDate.getDay();
  let workHours = Math.abs(endDate.getTime() - startDate.getTime()) / 3600000 / 3;
  let restDays = Math.floor(workHours / 8 / 7) * 2;
  workHours -= (restDays * 8);
  return workHours;
}

Please anyone help. thanks.

Daniel.Wang
  • 2,242
  • 2
  • 9
  • 27

1 Answers1

0

I would optimize your problem so that I'd be working with full dates.

If you take your range of 2020-01-01 12:45:00 - 2028-10-06 23:10:33, then there are essentially three parts to this:

  1. 2020-01-01 12:45:00 - 2020-01-01 23:59:59
  2. 2020-01-02 00:00:00 - 2028-10-05 23:59:59
  3. 2020-10-06 00:00:00 - 2028-10-06 23:10:33

Calculating business hours in full days is easy: dayAmount * (dayEnd - dayStart) * 60

Use whatever method you can figure out to calculate minutes in part 1 and 3. Then just sum everything together.

Edit: On the other hand, you might even have to split it into weekly chunks since it might not be a full week. And you'd have to calculate the amount of holidays, unless you're fine with a naive approach.

Ringolds
  • 236
  • 1
  • 8
  • Thanks, Iet me try with your method. I have one question for it. `dayAmount * (dayEnd - dayStart) * 60` what is dayAmount? and there is no option for weekend in your method. – Daniel.Wang Jan 07 '21 at 15:36
  • dayAmount is the amount of valid business days within that time range. You'd have to come up with a way to calculate that. Not to mention dealing with public holidays. – Ringolds Jan 07 '21 at 15:41