curriculum/challenges/english/blocks/daily-coding-challenges-python/698a1a73ade5ac0e19180fa1.md
Given a start date and an end date, return the number of business days between the two.
"YYYY-MM-DD".count_business_days("2026-02-24", "2026-02-26") should return 3.
({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.
({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.
({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.
({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.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-24", "2027-02-24"), 262)`)
}})
def count_business_days(start, end):
return start
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