Source Code:
(back to article)
from datetime import datetime, timedelta # Parse the start and end dates start_date = datetime.strptime("2022-01-01", "%Y-%m-%d") end_date = datetime.strptime("2022-01-31", "%Y-%m-%d") # Initialize a counter num_weekdays = 0 # Iterate over the days between the two dates for i in range((end_date - start_date).days + 1): current_day = start_date + timedelta(days=i) # Increment the counter if the day is a weekday if current_day.weekday() < 5: num_weekdays += 1 print(num_weekdays) # prints 22
Result:
Report an issue