Skip to content

Instantly share code, notes, and snippets.

@xphong
Created May 4, 2016 16:46
Show Gist options
  • Save xphong/2fdffe2b1142035286f345f5310b1ce2 to your computer and use it in GitHub Desktop.
Save xphong/2fdffe2b1142035286f345f5310b1ce2 to your computer and use it in GitHub Desktop.
Get number of business days in a selected month with Moment.js
// Usage: getNumberOfBusinessDaysInMonth(moment(), moment().daysInMonth());
function getNumberOfBusinessDaysInMonth(date, days) {
var date = moment(date).startOf('month');
var numberOfBusinessDays = 0;
while (days > 0) {
date = date.add(1, 'days');
if (date.isoWeekday() !== 7 && date.isoWeekday() !== 1) {
numberOfBusinessDays += 1;
}
days -= 1;
}
return numberOfBusinessDays;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment