Counting business days in Javascript

I'm trying to count the number of business days between two dates in Javascript and haven't been able to get any of the libraries that I've used for this elsewhere to work in Retool.

I've tried loading date-fns with no success and the business days plugin for Moment also doesn't seem to work. Any help with either of those libraries or suggestions for a different method that works would be really appreciated!

@blim This can definitely be done, but one question first. Are the business days static? For example, all business days are always Monday, Tuesday, Wednesday, Thursday, Friday?

Yep, they're static.

@blim OK so using moment is possible but can you explain a bit more about what you want to accomplish?
Are you counting all business days for a certain range of time? All time? Backwards from today? Forwards from today? etc.. Not trying to be a PITA just want to help so the more I know, the more I can help :slight_smile:

Not a PITA at all! I appreciate the help. I'm trying to count the number of business days between a given start and end date.

Example: if I worked 30 hours between September 1st, 2022 and September 15th, 2022 and I only worked on business days (M, T, W, Th, F). How many hours per day did I average?

Going to PM you as I don't want to go back and forth in the forum....

1 Like

(@ScottR you are a lifesaver :pray: )

Stepping in here as well, just in case anyone else is curious! I was able to count business days using this code in a JS transformer:

function countBusinessDays(startDate, endDate) {
  var day = moment(startDate);
  var businessDays = 0;

  while (day.isSameOrBefore(endDate,'day')) {
    if (!=0 &&!=6) businessDays++;
  return businessDays;

return countBusinessDays({{dateRange1.value.start}}, {{dateRange1.value.end}})

Not sure if this is the best way, but it's certainly one way!

1 Like