Menu Close

Python Calendar Module

Python Calendar Module

In this article, you will learn all about the python calendar module with the help of examples. The calendar module is going to be very useful when you want to work with calendars.

In this guide, we will go through various Calendar useful functions related to the calendar module in Python. By default, the Calendar module uses Monday as the first day of the week but you can change it by using setfirstweekday() function.

Python Calendar Module

Python calendar module is used to work with calendars. In the calendar module, there are various functions and classes available which provide additional features.

In the calendar module by default, Monday is the first day of the week and Sunday is the last day of the week.
To use the calendar module you have to import the calendar module using the import keyword.

import calendar

Attributes of the calendar module

To get all the attributes and methods of the calendar module, use the Python dir() function.

Example


import calendar
x = dir(calendar)
print(x)

Display calendar of a given month

To display a calendar of a specific month of the year, use the month method which takes month and year as an argument.

Example


import calendar
yy = 2017
mm = 10
x = calendar.month(yy, mm)
print(x)

class calendar.Calendar

Python Calendar class creates a Calendar object. The calendar class provides various methods that are used for calendar data formatting.

Python calendar module methods


MethodsDescription
iterweekdays(year, month)Return iterator of weekday number
itermonthdates(year, month)Return the iterator of the month number ( 1- 12 )
itermonthdays(year, month)Return the iterator of the month ( 1- 12 ) of the specified year
itermonthdays2(year, month)Return a tuple consisting of the day of the month number and weekday number
itermonthdays3(year, month)Return a tuple consisting of the a year, a day and a day of the month numbers
itermonthdays4(year, month)Return a tuple consisting of the year, a month, a day of the month, and a day of the week returns a tuple consisting of the year, a day, and a day of the month numbers
monthsdatecalendar(year, month)Return list of weeks in the month of the year as full weeks
monthsdays2calendar(year, month)Return a list of seven tuples of day numbers and weekdays numbers
monthdayscalendar(year, month)Return a list of seven tuple of day numbers
yeardatescalendar(year, width=3)Return the list of weeks in the month
yeardays2calendar(year, width=3)Return the data for the specified year ready for formatting.
yeardayscalendar(year, width=Return the data for the specified year ready for formatting

Methods

Let’s understand all the methods of the calendar.Calendar class using examples.

iterweekdays()

This method is used to return the iterator of the weekday number that will be used for a week.

Example

import calendar

obj = calendar.Calendar(firstweekday = 0)
for day in obj.iterweekdays():
	print(day)

itermonthdates()

This method is used to return the iterator for the month (1-12) in the year.

Example

import calendar

yy = 2019
mm = 11

obj = calendar.Calendar()
for day in obj.itermonthdates(yy, mm):
	print(day)

if you want to change the first weekday then you have to pass firstweekday argument in Calendar class.

Example

import calendar

yy = 2019
mm = 11

obj = calendar.Calendar(firstweekday = 2)
for day in obj.itermonthdates(yy, mm):
	print(day)

itermonthdays()

itermonthdays() method is used to return the iterator of the month ( 1- 12 ) of the specified year.

Example

import calendar

yy = 2019
mm = 11

obj = calendar.Calendar()
for day in obj.itermonthdays(yy, mm):
	print(day)

itermonthdays2()

itermonthdays2() method is used to return the iterator of the month ( 1- 12 ) of the specified year. The days returned will be a tuple consisting of a day of the month number and week day number.

Example:

import calendar

yy = 2021
mm = 7

obj = calendar.Calendar()
for day in obj.itermonthdays2(yy, mm):
	print(day)

itermonthdays3()

itermonthdays3() method is used to return the iterator of the month ( 1- 12 ) of the specified year. The days returned will be a tuple consisting of a year, a month, and a day of the month numbers.

Example

import calendar

yy = 2021
mm = 7

obj = calendar.Calendar()
for day in obj.itermonthdays3(yy, mm):
	print(day)

monthdatescalendar()

monthdatescalendar() method is used to get the list of weeks in the month of the year as full weeks. Weeks are the list of seven datetime.date objects.

Example

import calendar

yy = 2019
mm = 11

obj = calendar.Calendar()
for day in obj.monthdatescalendar(yy, mm):
	print(day)

monthdays2calender(year, month)

Return a list of the weeks in the month of the year as full weeks. weeks are a list of seven tuples of day numbers and weekdays numbers.

Example

import calendar

yy = 2021
mm = 11

obj = calendar.Calendar()
for day in obj.monthdays2calendar(yy, mm):
	print(day)

monthdayscalender(year, month)

Return a list of the weeks in the month of the year as full weeks. weeks are a list of seven tuples of day numbers.

Example

import calendar

year = 2021
month = 11

obj = calendar.Calendar()
for day in obj.monthdayscalendar(year, month):
	print(day)

yearsdatescalendar(year, width=3)

Return the data for the specified year ready for formatting.

Example:

import calendar
year = 2021

obj = calendar.Calendar()
for i in obj.yeardatescalendar(year, width=3):
	print(i)

yeardayscalendar(year, width=3)

Return the data for the specified year ready for formatting. Similar to yeardatescalendar().

Example

import calendar
year = 2021

obj = calendar.Calendar()
for i in obj.yeardayscalendar(year, width=3):
	print(i)

class calendar.TextCalendar

TextCalendar class is used to generate the plain text calendars.

Methods

TextCalendar class provides various methods and attributes.

formatmonth(theyear, themonth, w = 0, l = 0)

This method is used to get the month’s calendar in a multi-line string. It specifies the width of date columns that are centered. If l is given, it specifies the number of lines that each week will use.

Example:

import calendar

text = calendar.TextCalendar(firstweekday = 0)
year = 2019
month = 12

#formatmonth() with two parameters
print(text.formatmonth(year, month))

#formatmonth() with three parameters
print(text.formatmonth(year, month, w = 3))

#formatmonth() with four parameters
print(text.formatmonth(year, month, 3, 2))

prmonth(theyear, themonth, w = 0, l = 0)

The prmonth() method is used to print a month’s calendar as returned by formatmonth().

Example:

import calendar

text = calendar.TextCalendar(firstweekday = 0)
year = 2019
month = 12

#prmonth() with two parameters
text.prmonth(year, month)

#prmonth() with three parameters
text.prmonth(year, month, w = 3)

#prmonth() with four parameters
text.prmonth(year, month, 3, 2)

formatyear(theyear, w=2, l=1, c=6, m=3)

This method is used to get the month’s calendar in multi-line string.It specifies the width of date columns which are centered.If l is given, it specifis number of line that each week will use.

Example:

import calendar

text = calendar.TextCalendar(firstweekday = 0)
year = 2019

# month calender without width and line
print(text.formatyear(year))
# month calender with width
print(text.formatyear(year, w = 3))

# month calender with width and line
print(text.formatyear(year,3, 2))

pryear(theyear, w=2, l=1, c=6, m=3)

Print the calendar of the entire year as returned by the formatyear() method.

Example:

import calendar

text = calendar.TextCalendar(firstweekday = 0)
year = 2019

# month calender without width and line
text.pryear(year)
# month calender with width
text.pryear(year, w = 3)

# month calender with width and line
text.pryear(year,3, 2)

class calendar.HTMLCalendar

Python calendar HTMLCalendar class is used to generate HTML calendar.

formatmonth(theyear, themonth, withyear=True)

This can be used to generate month calendar as a HTML table.

Example:

import calendar

# create the object of HTMLCalendar class
text = calendar.HTMLCalendar(firstweekday=0)
year = 2020
month = 7

# print generated html calendar
print(text.formatmonth(2020, 7, withyear=True))

formatyear(theyear, withyear=True)

This can be used to return years’s HTML table.

Example

import calendar

text = calendar.HTMLCalendar(firstweekday=0)
year = 2020

# generate html table of entire year
print(text.formatyear(year, width=3))

HTMLCalendar has the following attributes you can override to customize the CSS classes used by the calendar.

cssclasses

cssclasses returns the list of CSS classes.

Example

import calendar

text = calendar.HTMLCalendar(firstweekday=0)
year = 2020

# list of the css classes.
print(text.cssclasses)

cssclasses_weekday_head

cssclasses_weekday_head property returns the CSS classes used for weekday names in the header now. This works the same as cssclasses.

Example:

import calendar

text = calendar.HTMLCalendar(firstweekday=0)
year = 2020

# list of the css classes.
print(text.cssclass_month_head)

Functions

The calendar module in Python provides various functions to work with calendars.

setfirstweekday()

Python calendar module treats Monday as the first day of the week but you can change it by using setfirstweekday() function.

Example

import calendar
calendar.setfirstweekday(2)

firstweekday()

Return the current setting for the weekday to start the week.

Example

import calendar
result = calendar.firstweekday()
print("The first weekday is:- ", result)

isleap()

This function is used to return True if the specified year is a leap year.

Example

import calendar
result = calendar.isleap(2020)
print("The result is:- ", result)

leapdays(y1, y2)

This function is used to return the number of leap years between given years. Here y1 and y2 represent the years.

Example

import calendar

leap_years = calendar.leapdays(1998, 2020)
print(leap_years)

Conclusion

That’s all about the Python calendar module. The calendar module in Python is going to be very helpful when you want to work with calendar-related tasks because Python calendar module provides classes, methods, properties, and functions to work with calendar.

In this guide, we have covered approx all the useful classes and methods that are helpful when you will work with a calendar.

If you like this Python calendar module tutorial article, please share and keep visiting for further Python tutorials.

Reference of Python calendar module:- Click Here

Thanks for reading.

Python Membership and Identity Operators
How to Convert a Tuple to a String in Python

Related Posts