Tuesday, July 26, 2011

Calculating working days between two dates with Javascript

Here’s a script that will calculate the number of working days between two Date objects in Javascript.  I had a hard time finding an accurate one online so I wrote one myself – please use with caution as it has not been rigorously tested.
Note that the calculation considers the difference between today and today to equal 1.  I’m using this to present the number of days left before an event, and in my case on the day of the event it makes sense to see “1 day” instead of “0 days.”  
function workingDaysBetweenDates(startDate, endDate) {
    // Validate input
    if (endDate < startDate)
        return 0;
    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0,0,0,1);  // Start just after midnight
    endDate.setHours(23,59,59,999);  // End just before midnight
    var diff = endDate - startDate;  // Milliseconds between datetime objects    
    var days = Math.ceil(diff / millisecondsPerDay);
    // Subtract two weekend days for every week in between
    var weeks = Math.floor(days / 7);
    days = days - (weeks * 2);

    // Handle special cases
    var startDay = startDate.getDay();
    var endDay = endDate.getDay();
    // Remove weekend not previously removed.   
    if (startDay - endDay > 1)         
        days = days - 2;      
    // Remove start day if span starts on Sunday but ends before Saturday
    if (startDay == 0 && endDay != 6)
        days = days - 1  
    // Remove end day if span ends on Saturday but starts after Sunday
    if (endDay == 6 && startDay != 0)
        days = days - 1  
    return days;



Unknown said...

Hi ken,

Just wanted to drop a note to say this is the most concise and straight forward example I've seen. I's also the most accurate, so many thanks!!


wabush said...

Several examples on stack overflowdid not work - this one did, thanks!

Unknown said...

Thanks. This is exactly what I was looking for.

Chris said...

I have been using this in my organization and I found an issue when the dates are '03/31/2015' to '04/13/2015'. It returns 11 instead of 9.

I found the bug to be: "if (startDay - endDay > 1) days = days - 2;".

It should be "if (startDay - endDay > 0) days = days - 2;".

But otherwise, perfect code man!

Kyaw Khaung said...

Thank a lot. You are my life saver for this code.

Erick Christian Rosales Cruz said...

Thanks for share! It works great!

Anonymous said...

great, but needs to be fixed... because when you put hours of startDate as 0,0,0,1 and endDate hours as 23,59,59,999 then 2 milliseconds you'll lose...for example if you will try to calculate working days between 2 month then you will get more days than expected because of 2 milliseconds ))

BenH said...

Ignore Chris, he is incorrect. If you make his proposed changes, the code will break. The original post is correct, that is if you're trying to take out both Saturday and Sunday. I tried the code with the original post and in the example Chris posts, the answer is 10.

Anonymous said...

Let Javascript do the hard work.

function getWeekDays(first, last) {
if (first > last) return -1;
var start = new Date(first.getTime());
var end = new Date(last.getTime());
var count = 0;
while (start <= end) {
if (start.getDay() != 0 && start.getDay() != 6)
start.setDate(start.getDate() + 1);
return count;

Unknown said...

Great function! Added to my library :)

Unknown said...

Great function, thanks!!
I modified it slightly to consider 3 different results:
1.- your result (number of monday through friday days between dates)
2.- total number of days (not substracting saturdays and sundays)
3.- number of working days (not considering saturdays, sundays or holidays... if a holiday falls on a saturday or sunday, it's substracted only once)