API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Number Of Weekdays Between Dates
The @Formula function for computing the number of weekdays between two dates (found here) came in handy when developing a JavaScript version of the same function.

function weekdaysBetween(startDate, endDate) {
   if (startDate < endDate) {
      var s = startDate;
      var e = endDate;
   } else {
      var s = endDate;
      var e = startDate;
   }
   var diffDays = Math.floor((e - s) / 86400000);
   var weeksBetween = Math.floor(diffDays / 7);
   if (s.getDay() == e.getDay()) {
      var adjust = 0;
   } else if (s.getDay() == 0 && e.getDay() == 6) {
      var adjust = 5;
   } else if (s.getDay() == 6 && e.getDay() == 0) {
      var adjust = 0;
   } else if (e.getDay() == 6 || e.getDay() == 0) {
      var adjust = 5-s.getDay();
   } else if (s.getDay() == 0 || s.getDay() == 6) {
      var adjust = e.getDay();
   } else if (e.getDay() > s.getDay() ) {
      var adjust = e.getDay()-s.getDay();
   } else {
      var adjust = 5+e.getDay()-s.getDay();
   }
   return (weeksBetween * 5) + adjust;
}


It follows the same process as the formula language version, so only a quick review is needed. Set up variables s and e to be the starting and the ending date in the correct order (in case the passed-in end date is actually before the passed-in start date). Then compute the number of days between the dates. JavaScript dates are in milliseconds, so 86,400,000 is the number of milliseconds in a day. Then compute the number of full weeks between the dates. Then compute the adjustment, using the same logic as the formula language. However, note that in Notes formula languge, Sunday is weekday 1, but in JavaScript Sunday is weekday 0. So the numbers have to be adjusted slightly, even though the logic is the same.