calendar

The Python calendar module provides classes and functions for calendar operations, including generating calendars in various formats and calculating dates.

You can use this module to generate calendars for a specific month or year and perform date-related calculations.

Here’s an example:

Python
>>> import calendar

>>> print(calendar.month(2025, 6))
     June 2025
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

Key Features

  • Generates plain text and HTML calendars for months and years
  • Calculates weekday and leap year information
  • Provides utility functions for working with dates

Frequently Used Classes and Functions

Object Type Description
calendar.Calendar Class Provides the base class for calendar-related operations
calendar.TextCalendar() Class Generates plain text calendars
calendar.month() Function Returns a month’s calendar as a multi-line string
calendar.isleap() Function Determines if a year is a leap year
calendar.weekday() Function Returns the day of the week for a given date

Examples

Generate a plain text calendar for a year:

Python
>>> print(calendar.calendar(2025))
                                  2025

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
       1  2  3  4  5                      1  2                      1  2
 6  7  8  9 10 11 12       3  4  5  6  7  8  9       3  4  5  6  7  8  9
13 14 15 16 17 18 19      10 11 12 13 14 15 16      10 11 12 13 14 15 16
20 21 22 23 24 25 26      17 18 19 20 21 22 23      17 18 19 20 21 22 23
27 28 29 30 31            24 25 26 27 28            24 25 26 27 28 29 30
                                                    31
...

Check if a year is a leap year:

Python
>>> calendar.isleap(2024)
True

Find out the weekday of a specific date:

Python
>>> calendar.weekday(2025, 6, 19)
calendar.THURSDAY

Common Use Cases

  • Displaying calendars for a specific month or year
  • Calculating the day of the week for any date
  • Checking for leap years

Real-World Example

Suppose you want to create a schedule that highlights all Mondays in a given month of a year. This example demonstrates how you can use the calendar module to achieve this:

Python
>>> import calendar
>>> year = 2025
>>> month = 6
>>> cal = calendar.TextCalendar(calendar.MONDAY)

>>> for day in cal.itermonthdays(year, month):
...     if day != 0:  # Non-zero means a valid day in the month
...         weekday = calendar.weekday(year, month, day)
...         if weekday == calendar.MONDAY:
...             print(f"Monday: {day}")
...
Monday: 2
Monday: 9
Monday: 16
Monday: 23
Monday: 30

This example uses the calendar module to iterate over the days of a given month, identifying all Mondays and printing them.

Tutorial

The Python calendar Module: Create Calendars With Python

Learn to use the Python calendar module to create and customize calendars in plain text, HTML or directly in your terminal.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated June 23, 2025