Back to Freecodecamp

Challenge 198: Business Day Count

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/698a1a73ade5ac0e19180fa1.md

latest1.5 KB
Original Source

--description--

Given a start date and an end date, return the number of business days between the two.

  • Given dates are in the format "YYYY-MM-DD".
  • Weekdays are business days (Monday through Friday).
  • Weekends are not business days (Saturday and Sunday).
  • Include both the start and end dates when counting.

--hints--

countBusinessDays("2026-02-24", "2026-02-26") should return 3.

js
assert.equal(countBusinessDays("2026-02-24", "2026-02-26"), 3);

countBusinessDays("2026-02-24", "2026-02-28") should return 4.

js
assert.equal(countBusinessDays("2026-02-24", "2026-02-28"), 4);

countBusinessDays("2026-02-21", "2026-03-01") should return 5.

js
assert.equal(countBusinessDays("2026-02-21", "2026-03-01"), 5);

countBusinessDays("2026-03-08", "2026-03-17") should return 7.

js
assert.equal(countBusinessDays("2026-03-08", "2026-03-17"), 7);

countBusinessDays("2026-02-24", "2027-02-24") should return 262.

js
assert.equal(countBusinessDays("2026-02-24", "2027-02-24"), 262);

--seed--

--seed-contents--

js
function countBusinessDays(start, end) {

  return start;
}

--solutions--

js
function countBusinessDays(start, end) {
  const startDate = new Date(start + "T00:00:00Z");
  const endDate = new Date(end + "T00:00:00Z");

  let count = 0;
  let current = new Date(startDate);

  while (current <= endDate) {
    const day = current.getUTCDay();
    if (day !== 0 && day !== 6) count++;
    current.setUTCDate(current.getUTCDate() + 1);
  }

  return count;
}