Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Calendar Module

  • Difficulty Level : Basic
  • Last Updated : 02 Jul, 2021

Python defines an inbuilt module calendar that handles operations related to the calendar. 
The calendar module allows output calendars like the program and provides additional useful functions related to the calendar. Functions and classes defined in the Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention).
Example #1: Display the Calendar of a given month.
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Python3




# Python program to display calendar of
# given month of the year
   
# import module
import calendar
   
yy = 2017
mm = 11
   
# display the calendar
print(calendar.month(yy, mm))

Output: 
 



Example #2: Display calendar of the given year.
 

Python3




# Python code to demonstrate the working of
# calendar() function to print calendar
   
# importing calendar module
# for calendar operations
import calendar
   
# using calendar to print calendar of year
# prints calendar of 2018
print ("The calendar of year 2018 is : ")
print (calendar.calendar(2018, 2, 1, 6))

Output: 
 

  
class calendar.Calendar : 
The calendar class creates a Calendar object. A Calendar object provides several methods that can be used for preparing the calendar data for formatting. This class doesn’t do any formatting itself. This is the job of subclasses. Calendar class allows the calculations for various tasks based on date, month, and year. Calendar class provides the following methods:

 

FunctionDescription
iterweekdays()Returns an iterator for the week day numbers that will be used for one week
itermonthdates()Returns an iterator for the month (1–12) in the year
itermonthdays()Returns an iterator of a specified month and a year
itermonthdays2()Method is used to get an iterator for the month in the year similar to itermonthdates(). Days returned will be tuples consisting of a day of the month number and a week day number.
itermonthdays3()Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month and a day of the month numbers.
itermonthdays4()Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month, a day of the month, and a day of the week numbers.
monthdatescalendar()Used to get a list of the weeks in the month of the year as full weeks
monthdays2calendar()Used to get a list of the weeks in the month of the year as full weeks
monthdayscalendarUsed to get a list of the weeks in the month of the year as full weeks
yeardatescalendar()Used to get a list of the weeks in the month of the year as full weeks
yeardays2calendar()Used to get the data for specified year. Entries in the week lists are tuples of day numbers and weekday numbers
yeardayscalendar()Used to get the data for specified year. Entries in the week lists are day numbers

  
class calendar.TextCalendar : 
TextCalendar class can be used to generate plain text calendars. TextCalendar class in Python allows you to edit the calendar and use it as per your requirement. 

 

FunctionDescription
formatmonth()Method is used to get month’s calendar in a multi-line string
prmonth()Method is used to print a month’s calendar as returned by formatmonth()
formatyear()Method is used to get m-column calendar for an entire year as a multi-line string
pryear()Method is used to print the calendar for an entire year as returned by formatmonth()

  
class calendar.HTMLCalendar : 
HTMLCalendar class can be used to generate HTML calendars. HTMLCalendar class in Python allows you to edit the calendar and use as per your requirement. 

 

FunctionDescription
formatmonth()Method is used to get month’s calendar as an HTML table
formatyear()Method is used to get year’s calendar as an HTML table.
formatyearpage()Method is used to get year’s calendar as a complete HTML page

  
Simple TextCalendar class :
For simple text calendars calendar module provides the following functions : 

 

FunctionDescription
setfirstweekday()Function sets the day start number of week
firstweekday()Function returns the first week day number. By default 0 (Monday)
isleap()Function checks if year mentioned in argument is leap or not
leapdays()Function returns the number of leap days between the specified years in arguments
weekday()Function returns the week day number(0 is Monday) of the date specified in its arguments
weekheader()Returns a header containing abbreviated weekday names
monthrange()Function returns two integers, first, the starting day number of week(0 as monday), second, the number of days in the month
monthcalendar()Returns a matrix representing a month’s calendar. Each row represents a week; days outside of the month are represented by zeros
prmonth()Function also prints the month of specific year but there is no need of “print” operation to execute this
month()Function prints the month of a specific year mentioned in arguments
prcal()Function also prints the calendar of specific year but there is no need of “print” operation to execute this
calendar()Function displays the year, width of characters, no. of lines per week and column separations.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :