Back to Freecodecamp

Challenge 198: Business Day Count

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

latest1.9 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--

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

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-24", "2026-02-26"), 3)`)
}})

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

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-24", "2026-02-28"), 4)`)
}})

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

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-21", "2026-03-01"), 5)`)
}})

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

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-03-08", "2026-03-17"), 7)`)
}})

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

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-24", "2027-02-24"), 262)`)
}})

--seed--

--seed-contents--

py
def count_business_days(start, end):

    return start

--solutions--

py
from datetime import datetime, timedelta

def count_business_days(start, end):
    start_date = datetime.strptime(start, "%Y-%m-%d")
    end_date = datetime.strptime(end, "%Y-%m-%d")
    
    count = 0
    current = start_date
    
    while current <= end_date:
        if current.weekday() < 5:
            count += 1
        current += timedelta(days=1)
    
    return count