fork(15) download
  1. function getBusinessDateCount (startDate, endDate) {
  2. var elapsed, daysBeforeFirstSaturday, daysAfterLastSunday;
  3. var ifThen = function (a, b, c) {
  4. return a == b ? c : a;
  5. };
  6.  
  7. elapsed = endDate - startDate;
  8. elapsed /= 86400000;
  9.  
  10. daysBeforeFirstSunday = (7 - startDate.getDay()) % 7;
  11. daysAfterLastSunday = endDate.getDay();
  12.  
  13. elapsed -= (daysBeforeFirstSunday + daysAfterLastSunday);
  14. elapsed = (elapsed / 7) * 5;
  15. elapsed += ifThen(daysBeforeFirstSunday - 1, -1, 0) + ifThen(daysAfterLastSunday, 6, 5);
  16.  
  17. return Math.ceil(elapsed);
  18.  
  19. /* 2015-04-29 til 2015-04-29 will return 1
  20.   if you want it to return 0 instead 1:
  21.   change Math.ceil() to parseInt() in last line */
  22. }
  23.  
  24.  
  25. var date1 = new Date(1999, 12, 31);
  26. var date2 = new Date(); // now
  27.  
  28. print( getBusinessDateCount(date1, date2) );
  29.  
  30.  
Success #stdin #stdout 0.43s 321856KB
stdin
Standard input is empty
stdout
4143