Python Program to Display Calendar

Updated on September 30, 2024
Display Calendar header image

Introduction

A calendar display in Python can be both a practical and educational tool, helping you understand date and time manipulation while offering real-world utility. Python, with its rich library ecosystem, simplifies tasks like calendar creation with modules specifically designed for this purpose.

In this article, you will learn how to display a calendar using Python. This will cover several methods, from displaying a single month to showing an entire year, using the built-in calendar module. You'll see various examples that demonstrate how to customize the output to suit different needs.

Displaying a Month's Calendar

Display the Current Month

  1. Import the calendar module.

  2. Retrieve the current year and month.

  3. Use the calendar.month() function to generate the calendar for the current month.

    python
    import calendar
    from datetime import datetime
    
    now = datetime.now()
    print(calendar.month(now.year, now.month))
    

    This code snippet displays the calendar for the current month. The datetime.now() method helps fetch the current year and month, which are then passed to calendar.month() to render the calendar.

Display Any Specific Month

  1. Set the year and month you want to display.

  2. Generate the month’s calendar using similar steps as above.

    python
    import calendar
    
    year = 2023
    month = 9  # September
    print(calendar.month(year, month))
    

    Here, calendar.month() is used to display the calendar for September 2023. You simply change the year and month variables to reflect the month you want to view.

Displaying a Year's Calendar

Display the Current Year

  1. Import the calendar module.

  2. Retrieve the current year.

  3. Use the calendar.calendar() function to print the calendar for the entire year.

    python
    import calendar
    from datetime import datetime
    
    current_year = datetime.now().year
    print(calendar.calendar(current_year))
    

    This code prints the entire calendar for the current year. datetime.now().year provides the current year, and calendar.calendar() generates the full year's calendar.

Display Any Specific Year

  1. Specify the year you want to display.

  2. Use calendar.calendar() to generate the calendar for that year.

    python
    import calendar
    
    year = 2025
    print(calendar.calendar(year))
    

    Adjust year to generate the calendar for any specific year, in this case, 2025.

Using Calendar Options for Customization

Adjust the Week's Start Day

  1. Use the calendar.setfirstweekday() method to define the starting day of the week.

  2. Generate a month's or year's calendar as shown previously.

    python
    import calendar
    
    calendar.setfirstweekday(calendar.SUNDAY)
    print(calendar.month(2023, 9))
    

    By setting the first weekday to calendar.SUNDAY, all calendar displays will start the week on Sunday. This is customizable per your regional or personal preference.

Display a Text Calendar with Monday Starting

  1. Create a TextCalendar instance.

  2. Set Monday as the starting day of the week.

  3. Format and display the month.

    python
    import calendar
    
    tc = calendar.TextCalendar(firstweekday=calendar.MONDAY)
    print(tc.formatmonth(2023, 9))
    

    This method gives more control over formatting and allows setting the first weekday directly in the TextCalendar instance.

Conclusion

The calendar module in Python serves as a flexible tool for creating and displaying calendars. Whether you need a calendar for the current month, a specific year, or require customization, such as changing the week's start day, Python's standard library covers these needs with ease. By employing the techniques discussed, you can efficiently manage date-related functionalities in your applications, ensuring they cater to diverse requirements and preferences.