Thursday, July 4, 2024
HomeLanguagesPythonPython | Calendar Module

Python | Calendar Module

Python defines an inbuilt module calendar that handles operations related to the calendar. 
The calendar module allows us to 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.
 

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 the 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))


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. The calendar class allows the calculations for various tasks based on date, month, and year and provides the following methods:
 

Function Description
iterweekdays() Returns an iterator for the weekday 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() This 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
monthdayscalendar Used 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. 
 

Function Description
formatmonth() This method  is used to get the month’s calendar in a multi-line string
prmonth() This method  is used to print a month’s calendar as returned by formatmonth()
formatyear() This method is used to get m-column calendar for an entire year as a multi-line string
pryear() This 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. 
 

Function Description
formatmonth() This method  is used to get the month’s calendar as an HTML table
formatyear() This method is used to get a year’s calendar as an HTML table.
formatyearpage() This method is used to get the year’s calendar as a complete HTML page

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

Function Description
setfirstweekday() This method sets the day start number of the week
firstweekday() This method returns the first weekday number. By default 0 (Monday)
isleap() This method checks if the year mentioned in the argument is leap or not
leapdays() This method returns the number of leap days between the specified years in arguments
weekday() This method returns the weekday number(0 is Monday) of the date specified in its arguments
weekheader() Returns a header containing abbreviated weekday names
monthrange() The function returns two integers, first, the starting day number of the 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() The function also prints the month of a specific year but there is no need of “print” operation to execute this
month() The function prints the month of a specific year mentioned in arguments
prcal() The function also prints the calendar of a specific year but there is no need of “print” operation to execute this
calendar() The function displays the calendar for the given year.

 

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments