Generate Holiday Calendars for Different Countries in Python
Generate a sorted list of public holidays for a given country and year using Python's calendar and datetime modules.
Python code
37 linesimport calendar
from datetime import date, timedelta
def generate_holiday_calendar(country_code, year=2025):
holidays = []
if country_code == "US":
# New Year's Day
holidays.append(date(year, 1, 1))
# Independence Day
holidays.append(date(year, 7, 4))
# Thanksgiving (4th Thursday of November)
cal = calendar.monthcalendar(year, 11)
thanksgiving = cal[3][3] if cal[0][3] == 0 else cal[4][3]
holidays.append(date(year, 11, thanksgiving))
# Christmas Day
holidays.append(date(year, 12, 25))
elif country_code == "UK":
# New Year's Day
holidays.append(date(year, 1, 1))
# Good Friday (approximate: day before Easter Sunday, simplified as last Friday in March)
cal = calendar.monthcalendar(year, 3)
good_friday = max([week[4] for week in cal if week[4] != 0])
holidays.append(date(year, 3, good_friday))
# Christmas Day
holidays.append(date(year, 12, 25))
# Boxing Day
holidays.append(date(year, 12, 26))
else:
print(f"No predefined holidays for country code: {country_code}")
return []
return sorted(holidays)
if __name__ == "__main__":
print("US Holidays 2025:", generate_holiday_calendar("US", 2025))
print("UK Holidays 2025:", generate_holiday_calendar("UK", 2025))
Output
US Holidays 2025: [datetime.date(2025, 1, 1), datetime.date(2025, 7, 4), datetime.date(2025, 11, 27), datetime.date(2025, 12, 25)]
UK Holidays 2025: [datetime.date(2025, 1, 1), datetime.date(2025, 3, 28), datetime.date(2025, 12, 25), datetime.date(2025, 12, 26)]
How it works
The function uses Python's built-in datetime.date and calendar module to compute fixed and floating holidays. Fixed holidays like New Year's Day and Christmas are appended directly. Floating holidays (e.g., Thanksgiving, Good Friday) are computed using calendar.monthcalendar to find the correct weekday occurrence (e.g., 4th Thursday) within a month. The list is sorted before returning so holidays appear in chronological order. The function falls back with a message for unsupported country codes.
Common mistakes
- Forgetting that calendar.monthcalendar returns weeks that start on Monday (index 0 = Monday).
- Assuming every month has exactly 5 weeks and indexing blindly without checking for zeros.
- Hardcoding holiday dates without considering that some holidays (e.g., Easter) change every year.
- Not sorting the final list of holidays before returning.
Variations
- Use the `holidays` library for more comprehensive and accurate holiday data across many countries.
- Extend to support additional floating holidays like Easter Monday or Memorial Day using recurrence rules.
Real-world use cases
- Generating a company-wide time-off calendar for payroll and HR systems based on local holidays.
- Building a scheduling tool that avoids booking meetings on public holidays in the user's region.
- Automating email or notification reminders about upcoming holidays in a multi-country team.
Sponsored
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
Keep learning
Related tutorials and quizzes for this topic.