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.
Import the calendar
module.
Retrieve the current year and month.
Use the calendar.month()
function to generate the calendar for the current month.
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.
Set the year and month you want to display.
Generate the month’s calendar using similar steps as above.
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.
Import the calendar
module.
Retrieve the current year.
Use the calendar.calendar()
function to print the calendar for the entire year.
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.
Specify the year you want to display.
Use calendar.calendar()
to generate the calendar for that year.
import calendar
year = 2025
print(calendar.calendar(year))
Adjust year
to generate the calendar for any specific year, in this case, 2025.
Use the calendar.setfirstweekday()
method to define the starting day of the week.
Generate a month's or year's calendar as shown previously.
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.
Create a TextCalendar
instance.
Set Monday as the starting day of the week.
Format and display the month.
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.
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.