Python calendar Introduction














































Python calendar Introduction



Introduction to the Python Calendar Module



Python has an built-in module named Calendar that contains useful classes and functions to support a variety of calendar operations. By default, the Calendar module follows the Gregorian calendar, where Monday is the first day (0) of the week and Sunday is the last day of the week (6).

In Python, datetime and time modules also provide low-level calendar-related functionalities. In addition to these modules, the Calendar module provides essential functions related to displaying and manipulating calendars.

To print and manipulate calendars, the Calendar module has 3 important classes: CalendarTextCalendar, and HTMLCalendar. In this article, we will see how these classes can help implement a variety of calendar related functions.

To use the Calendar module, we need to first import the module using:

import calendar

Let's take a look at the list of useful functions in this module.

class calendar.Calendar :

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%u2019t do any formatting itself. This is the job of subclasses. Calendar class allows the calculations for various task 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%u201312) 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 as per your requirement.

FUNCTIONDESCRIPTION
formatmonth()Method is used to get month%u2019s calendar in a multi-line string
prmonth()Method is used to print a month%u2019s 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%u2019s calendar as an HTML table
formatyear()Method is used to get year%u2019s calendar as an HTML table.
formatyearpage()Method is used to get year%u2019s 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%u2019s 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 %u201Cprint%u201D 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 %u201Cprint%u201D operation to execute this
calendar()Function displays the year, width of characters, no. of lines per week and column separations.

More Articles of Anshuman Gupta:

Name Views Likes
Python calendar leapdays 744 11
Python functools cmp_to_key 1294 12
Python collections ordereddict 828 12
Python tkinter : Button 1057 12
Python calendar formatmonth 666 11
Python tkinter : Canvas 1145 11
Python tkinter Radiobutton 913 13
Python program to get the value of Pi to n number of decimal places. 1496 12
Python tkinter : Introduction 55 2
Python calendar firstweekday 730 12
Python calendar Introduction 805 12
Python tkinter Scrollbar 1007 13
Write a Python program to create a label and change the label font style (font name, bold, size) using tkinter module 1943 13
Python tkinter Menubutton 876 13
The most efficient way of generating prime numbers 873 12
Python functools total_ordering 747 14
Python tkinter : Entry 1020 12
Python calendar isleap 728 12
Python project to create a pages automatically like WordPress in Django from admin panel 1893 12
How to download and install Python Latest Version on Android 1083 12
Python calendar formatyear 683 12
Python re Metacharacters 748 12
Python tkinter Listbox 1043 14
Python re search 768 12
Python tkinter Frame 843 11
Python functools update_wrapper 939 12
Python collections deque 733 12
Python calendar setfirstweekday 649 13
Python tkinter Label 850 12
Python re subn 716 12
Python re Introduction 847 13
Python collections Introduction 736 12
Python calendar pryear 630 13
Python calendar prmonth 658 11
Best approach to find the factorial of a number 582 12
Python tkinter : Checkbutton 912 12
Python functools Wraps 688 12
Python re split 815 12
Python calendar iterweekdays 686 12
Python re findall 1007 12
Python re sub 750 12
Python functools Introduction 671 12
Python tkinter : Introduction 1417 12
python collections counter 839 12
Python functools Lru_cache 993 12
Python tkinter Scale 918 13
Python calendar Introduction 60 10
Python tkinter Spinbox 898 14
Python tkinter Message 822 13
Python tkinter Project 1050 13
Python collections chainmap 622 12
Python functools Reduce 714 12
Python calendar itermonthdates 1042 12
Python re match 785 12

Comments