Python calendar Module with Examples

Python calendar Module: In this tutorial, we are going to learn about the calendar module with its classes, methods, constants with examples in Python programming language. By Hritika Rajput Last updated : June 21, 2023

Python calendar Module

The methods and classes defined in this module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. Zero and negative years are interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is 2 BC. 

These calendars have Monday as the first day of the week, and Sunday as the last, by default. You can use the setfirstweekday() to set the first day of the week to Sunday (6) or any other weekday. Parameters that specify dates are given as integers.

Example 1: Python program to display the calendar of a given year

# Python program to print the calendar of 
# the given year
    
# importing calendar module   
import calendar  
    
# using calendar to print calendar of year  
print ("The calender of year 2020 is : ")  
print (calendar.calendar(2020, 2, 1, 6)) 

Output:

The calender of year 2020 is : 
                                  2020

      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
 6  7  8  9 10 11 12       3  4  5  6  7  8  9       2  3  4  5  6  7  8
13 14 15 16 17 18 19      10 11 12 13 14 15 16       9 10 11 12 13 14 15
20 21 22 23 24 25 26      17 18 19 20 21 22 23      16 17 18 19 20 21 22
27 28 29 30 31            24 25 26 27 28 29         23 24 25 26 27 28 29
                                                    30 31

       April                      May                       June
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  3       1  2  3  4  5  6  7
 6  7  8  9 10 11 12       4  5  6  7  8  9 10       8  9 10 11 12 13 14
13 14 15 16 17 18 19      11 12 13 14 15 16 17      15 16 17 18 19 20 21
20 21 22 23 24 25 26      18 19 20 21 22 23 24      22 23 24 25 26 27 28
27 28 29 30               25 26 27 28 29 30 31      29 30

        July                     August                  September
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  3  4  5  6
 6  7  8  9 10 11 12       3  4  5  6  7  8  9       7  8  9 10 11 12 13
13 14 15 16 17 18 19      10 11 12 13 14 15 16      14 15 16 17 18 19 20
20 21 22 23 24 25 26      17 18 19 20 21 22 23      21 22 23 24 25 26 27
27 28 29 30 31            24 25 26 27 28 29 30      28 29 30
                          31

      October                   November                  December
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                         1          1  2  3  4  5  6
 5  6  7  8  9 10 11       2  3  4  5  6  7  8       7  8  9 10 11 12 13
12 13 14 15 16 17 18       9 10 11 12 13 14 15      14 15 16 17 18 19 20
19 20 21 22 23 24 25      16 17 18 19 20 21 22      21 22 23 24 25 26 27
26 27 28 29 30 31         23 24 25 26 27 28 29      28 29 30 31
                          30

Example 2: Python program to display a given month

# Python program to print the calendar of 
# the given month
    
# importing calendar module   
import calendar  
    
# using calendar to print calendar of May 2020  
print ("The calender of May 2020 is : ")  
calendar.prmonth(2020, 5)

Output:

The calender of May 2020 is : 
      May 2020
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 31

Classes and Methods of Python calendar Module

Calendar module in Python provides the following classes,

1. Calendar class

This class creates a Calendar object. A Calendar object provides several methods that can be used for preparing the calendar data for formatting. The formatting of data is done by the subclasses of this class.

Method Description
iterweekdays() Returns an iterator for the week day numbers which will be used for one week.
itermonthdates(y, m) Returns an iterator for the month m (1–12) in the year y.
itermonthdays(y, m) Returns an iterator for the month m in the year y similar to itermonthdates(). Days returned are the day of the month numbers. For the days outside of the given month, the day number is 0.
itermonthdays2(y, m) Returns an iterator for the month m in the year y
Days returned are tuples consisting of a day of the month number and a weekday number.
itermonthdays3(y, m) Returns an iterator for the month m in the year y Days returned are tuples consisting of a year, a month and a day of the month numbers.
Available from version 3.7
itermonthdays4(y, m) Returns an iterator for the month m in the year y Days returned are tuples consisting of a year, a month, a day of the month, and a day of the week numbers.
Available from version 3.7
monthdatescalendar(y, m) Returns a list of the weeks in the month m of the year y as full weeks.
Weeks given as lists of seven datetime.date objects.
monthdays2calendar(y, m) Returns a list of the weeks in the month m of the year y as full weeks.
Weeks given as lists of seven tuples of day numbers and weekday numbers.
monthdayscalendar(y, m) Returns a list of the weeks in the month m of the year y as full weeks.
Weeks given as lists of seven-day numbers.
yeardatescalendar(y, width=3) Returns the data for the specified year y ready for formatting. The return value is a list of month rows.
yeardays2calendar(y, width=3) Returns the data for the specified year y ready for formatting.
Week lists are tuples of day numbers and weekday numbers.
yeardayscalendar(y, width=3) Returns the data for the specified year y ready for formatting.
Week lists given are lists of day numbers.

2. TextCalendar Class

This class creates simple text calendars.

Method Description
formatmonth(year, month, w=0, l=0) Returns a month's calendar in a multi-line string.
prmonth(year, month, w=0, l=0) Prints a month's calendar as returned by formatmonth().
formatyear(year, w=2, l=1, c=6, m=3) Returns a m-column calendar for an entire year as a multi-line string.
pryear(year, w=2, l=1, c=6, m=3) Prints the calendar for an entire year as returned by formatyear().

3. HTMLCalendar Class

This class is used to create HTML calendars and do operations on it. HTML Calendar has following methods and attributes:

Methods

Method Description
formatmonth(year, month, withyear=True) Returns a month's calendar as an HTML table.
formatyear(year, width=3) Returns a year's calendar as an HTML table.
formatyearpage(year, width=3, css='calendar.css', encoding=None) Returns a year's calendar as a complete HTML page.

Attributes

Attribute Description
cssclasses List of CSS classes used for each weekday.
cssclass_noday CSS class for a weekday occurring in the previous or coming month.
cssclasses_weekday_head List of CSS classes used for weekday names in the header row.
cssclass_month_head The month's head CSS class (used by formatmonthname()). The default value is "month".
cssclass_month CSS class for the whole month's table (used by formatmonth()). The default value is "month".
cssclass_year CSS class for the whole year's table of tables (used by formatyear()). The default value is "year".
cssclass_year_head CSS class for the table head for the whole year (used by formatyear()). The default value is "year".

4. LocaleTextCalendar Class

This is a subclass of TextCalendar class. It can be passed a locale name in the constructor which will return month and weekday names in the specified locale.

5. LocaleHTMLCalendar CLass

This is a subclass of HTMLCalendar class. It can be passed a locale name in the constructor which will return month and weekday names in the specified locale.

Calendar module in Python provides the following methods for simple text calendars:

Methods Description
setfirstweekday(weekday) Sets the weekday to start each week.
Where (0 is Monday, 6 is Sunday).
firstweekday() Returns the current setting for the weekday to start each week.
isleap(y) Returns True if y is a leap year, else False.
leapdays(y1, y2) Returns the number of leap years in the range from y1 to y2 , where y1 and y2 are years.
weekday(year, month, day) Returns the day of the week (0 is Monday) for year (1970–...), month (1–12), day (1–31).
weekheader(n) Return a header containing abbreviated weekday names. n specifies the width in characters for one weekday.
monthrange(year,month) Returns weekday of first day of the month and number of days in month, for the given year and month.
monthcalendar(year, month) Returns a matrix representing a month's calendar. Each row represents a week; days outside of the month are printed 0.
prmonth(year, month, w=0, l=0) Prints a month's calendar as returned by month().
month(year, month, w=0, l=0) Returns a month's calendar in a multi-line string using the formatmonth() of the TextCalendar class.
prcal(year, w=0, l=0, c=6, m=3) Prints the calendar for an entire year as returned by calendar().
calendar(year, w=0, l=0, c=6, m=3) Returns a 3-column calendar for an entire year as a multi-line string using the formatyear() of the TextCalendar class.

Data Attributes of Python calendar Module

This module has the following data attributes:

  1. day_name: It is an array that represents the days of the week in the current locale.
  2. day_abbr: It is an array that represents the abbreviated days of the week in the current locale.
  3. month_name: It is an array that represents the months of the year in the current locale. Here January is represented as month number 1, so the array has a length of 13 and month_name[0] being an empty string.
  4. month_abbr: It is an array that represents the abbreviated months of the year in the current locale. Here January is represented as month number 1, so the array has a length of 13 and month_abbr[0] being an empty string.

Reference: calendar — General calendar-related functions

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.