Python Calendar
Python provides an in-built module called Calendar. The Calendar module allows you to access useful functions related to dates, weeks, months, and years.
These functions use to perform various operations related to the calendar.
Besides these operations, the Calendar module has two classes: TextCalendar and HTMLCalendar. These classes make the calendar module easy to use as needed.
Let’s see what you can do with this Python module.
Getting Dates of a Month
The method itermonthdates(year, month) returns an iterator for the specified month of a year. This iterator contains dates in the format yyyy-mm-dd.
Example
In the above example, we used an if block to add the condition that the iterator variable should only be printed if i[6], i.e., the second digit of the month value is equal to “2”.
Whole Month on Python Calendar
If you want to print a specific month in the calendar format, you can do it by accessing the month(year, month) method inside the calendar module.
Let’s see how to print a month in Python.
Example
The calendar got printed in a nice organized manner, but what if you want to change the first day of the week. To change the first day of the week in the calendar, you can use the formatmonth() function of the TextCalendar class.
Example
As intended, the first day in the week of the day row changes to Sunday.
Print Calendar of a year in Python
The calendar() function provides the functionality to print a whole specified year in a formatted way in the output.
Example
If we exclude the year parameter, the calendar(year, w, l. c, m) method takes four parameters, they are all for customizing the output. Here is what they all do:
- w: This parameter controls the space between the columns in each month of the printed year.
- l: This parameter decides the spacing between the rows in each month of the printed year. This is a very sensitive parameter, optimal value is 1, increasing it can make the appearance of the calendar hard to read.
- c: Spacing between the columns of the whole calendar is decided by this parameter.
- m: By changing this parameter, you can change how many months are going to be in each row of the calendar. In our example, we chose the value equal to 3.
If you want you can make the output show all 12 months in a single row by changing the value of ‘m’ equal to 12.
Try tweaking these values and change the appearance of the output according to your need.
Calendar in HTML
The HTMLCalendar() in the calendar module allows you to print the calendar in the HTML format.
If you are a HTML developer, then it can give some extra control over the appearance of the calendar.
Example
Now, you can copy the output of the above code, edit it and use it wherever you want.
More Calendar module methods
1. calendar.Calendar Class method
This important class of the module provides you methods that make you able to perform tasks based on calendar attributes like dates, months, and years.
The first day of the week can be changed while instantanizing calendar.Calendar(firstweekday = 0).
Here are the methods:
Function | Function Description |
iterweekdays() | This function returns an iterator that contains all weekday numbers of a week. The first-week number would be equal to the firstweekday parameter set while instantanizing the class. |
itermonthdays(year, month) | The function returns an iterator that contains all the number of days in the specified month. |
itermonthdates(year, month) | Returns all the dates of the specified month of the year in the format yyyy-mm-dd. |
itermonthdays2(year, month) | This returns an iterator that contains two integer values in a tuple. The first value is the number of the day of the specified month. The second value is the number of the weekday. |
itermonthdays3(year, month) | This returns an iterator that contains the year, month and day as three individual values in a tuple. |
itermonthdays4(year, month) | This returns an iterator that contains the year, month, day, and the number of the weekday as four individual values in a tuple. |
yeardatescalendar(year, width) | Returns a list of the datetime.date() objects in the format (yyyy, month, day) for the specified year. |
yeardayscalendar(year, width) | Returns a list containing weeks of the specified year. Each item in the list contains weekday numbers of that week. The value of weekdays is zero if the days are outside of the specified year. |
yearsdays2calendar(year, width) | Returns the data of the specified year. The data contains lists that have tuples of day numbers of the month and weekday numbers. |
monthdatescalendar(year, month) | Returns a list of the datetime.date() objects in the format (yyyy, month, day) for the specified month. |
monthdayscalendar(year, month) | Returns a list containing weeks of the specified month. Each item in the list contains weekday numbers of that week. 4 rows for 4 weeks in a month. |
monthdays2calendar(year, month) | Returns the data of the specified month of the year. The data contains lists containing tuples of day numbers of the month and weekday numbers. |
2. Simple calendar functions
These functions are provided in the calendar module to perform simple operations on text calendars.
Function | Function Description |
month(year, month, w, l) | This function returns the formatted month of the specified year. |
prmonth(year, month, w, l) | This function prints the formatted month of the specified year. |
calendar(year, w, l, c, m) | Returns a 3 column calendar of the specified year by default. However, the number of columns can be changed. |
prcal(year, w, l, c, m) | This function prints the calendar of the specified year. A number of columns can be changed in the calendar output. |
leapdays(year1, year2) | This function returns the number of leap days between two years. |
leapdays(year) | Returns True if the specified year is a leap year. |
weekday(year, month, day) | This function returns the number of the weekday of the date passed as parameters. (Monday = 0) |
weekheader(n) | Returns the abbreviated names of all days in a week. The parameter decides the spacing between each name. |
setfirstweekday(weekday) | Sets the first day of the week. The default is Monday. |
firstweekday() | Returns the number of the first weekday. Default is 0 (Monday = 0) |
monthrange(year, month) | Returns the starting day and the number of days in the specified month as integer values. |
monthcalendar(year, month) | This function returns a matrix where each row is a complete week, and the days out of the specified month are shown as zeros. |
3. calendar.HTMLCalendar Class Methods
HTMLCalendar class has the below methods which is used to perform HTML related operations.
You can also change the first week of the day while instantanizing as below: calendar.HTMLCalendar(firstweekday = 1)
Below methods are used for HTMLCalendar:
Function | Function Description |
formatmonth(year, month, withyear=True) | Returns the HTML table of the specified month of the year. If the ‘withyear‘ parameter is True then the year will also be in the header. |
formatyear(year, width=3) | Returns the HTML table of the specified year. The ‘width‘ parameter specifies the number of months in each row. |
formateyearpage(year, width, css, encoding) | This method returns the specified year’s calendar as a complete HTML page. CSS is used to select the CSS file. The parameter ‘encoding’ is by default set to the system’s encoding (usually UTF-8). |
4.calendar.TextCalendar Class Methods
TextCalendar class functions generate text calendars that are editable. While instantanizing the class, you can pass the parameter to set the first day of the week (calendar.TextCalendar(firstweekday = 1)).
Here are the methods that are provided by this class:
Function | Function Description |
formatmonth(year, month, w, l) | This function returns a month’s calendar as a multi-line string. |
prmonth(year, month, w, l) | This function directly prints a month’s calendar as a multi-line string. |
formatyear(year, w, l, c, m) | Returns the formatted calendar of the specified string as a multiline string. The extra parameters are used to change the appearance of the output. |
pryear(year, w, l, c, m) | Directly prints the multiline string of the specified year. |