Skip to content

Instantly share code, notes, and snippets.

@jcbagtas
Last active November 11, 2023 18:46
Show Gist options
  • Save jcbagtas/8c622d294fe8ad12bb4af59f8543ce71 to your computer and use it in GitHub Desktop.
Save jcbagtas/8c622d294fe8ad12bb4af59f8543ce71 to your computer and use it in GitHub Desktop.
Calculate Working Days between two dates. Javascript equivalent of Microsoft Excel's NETWORKDAYS() function.
/****
Javascript equivalent of Microsoft Excel's NETWORKDAYS() function.
Calculate Working Days between two dates.
Optimized for performance. accepts String '12/18/1990' and new Date('12/18/1990') Object.
usage:
networkdays('12/18/1990','12/18/2016');
networkdays(new Date(),'12/18/2016');
http://bgtsworks.blogspot.com/
*****/
networkdays = function(startDate,endDate){
var startDate = typeof startDate=='object' ? startDate: new Date(startDate);
var endDate = typeof endDate=='object' ? endDate: new Date(endDate);
if (endDate > startDate){
var days = Math.ceil((endDate.setHours(23,59,59,999) - startDate.setHours(0,0,0,1)) / (86400 * 1000));
var weeks = Math.floor(Math.ceil((endDate.setHours(23,59,59,999) - startDate.setHours(0,0,0,1)) / (86400 * 1000)) / 7);
days = days - (weeks * 2);
days = startDate.getDay() - endDate.getDay() > 1 ? days - 2 : days;
days = startDate.getDay() == 0 && endDate.getDay() != 6 ? days - 1 : days;
days = endDate.getDay() == 6 && startDate.getDay() != 0 ? days - 1 : days;
return days;
}
return null;
};
@Charles-Schleich
Copy link

Lekker ! thanks man ! 👍

@jhmchd
Copy link

jhmchd commented Sep 2, 2019

Should return 1 for endDate = startDate ?

@jcbagtas
Copy link
Author

jcbagtas commented Sep 9, 2019

Should return 1 for endDate = startDate ?

A dusty 3 year-old snippet is still relevant I see. lol
You are right. There should be a return, not just null. But I guess I had a reason why it's null, I can't quite remember now. lol

Lekker ! thanks man ! 👍
No prob.

@aaa-manuelhe
Copy link

Sweet! Just what I was looking for. Thanks!

@jcbagtas
Copy link
Author

jcbagtas commented Mar 8, 2021

Sweet! Just what I was looking for. Thanks!

glad this still helps. cheers!

@leon340
Copy link

leon340 commented May 19, 2021

when calculating days, Math.ceil returns the wrong number of days when the input is as follows:

startDate - Mon Nov 01 2021 00:00:00 GMT-0700 (Pacific Daylight Time)
endDate - Tue Nov 30 2021 23:59:59 GMT-0800 (Pacific Standard Time)
days - 30.041666643518518

Would Math.round be a better option? Or is the above input incorrect?

@leonuriah
Copy link

what does (86400 * 1000) represent?

@jcbagtas
Copy link
Author

when calculating days, Math.ceil returns the wrong number of days when the input is as follows:

startDate - Mon Nov 01 2021 00:00:00 GMT-0700 (Pacific Daylight Time)
endDate - Tue Nov 30 2021 23:59:59 GMT-0800 (Pacific Standard Time)
days - 30.041666643518518

Would Math.round be a better option? Or is the above input incorrect?

You may be right, rounding off days can be useful most of the times.

what does (86400 * 1000) represent?

Milliseconds per day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment