You are on page 1of 11

(/)

Python CALENDAR Tutorial with Example


Calendar module in Python has the calendar
class that allows the calculations for various
task based on date, month, and year. On top
of it, the TextCalendar and HTMLCalendar
class in Python allows you to edit the calendar
and use as per your requirement.

Let see what we can do with Python Calendar.

Step1) Run the code.

(//cdn.guru99.com/images/Pythonnew/Python16.1.png)

Code Line # 1: We begin with "import calendar" which will import all the classes of this
module.
Code Line # 3: c= calendar.TextCalendar(calendar.SUNDAY) tells the interpreter to create
a text calendar. Start of the month will be Sunday. In Python, you can format the
calendar as you can change the day of the month to begin with
Code Line # 4: str= c.formatmonth(2025,1) We are creating calendar for the year 2025,
Month 1 – January
Code Line # 5: print str will print the output.
Let's quickly change the value from Sunday to Thursday and check the output

(//cdn.guru99.com/images/Pythonnew/Python16.2.png)

Step 2) You can also print out the Calendar in HTML format, this feature is helpful for
developer if they want to make any changes in the look and feel of calendar

(//cdn.guru99.com/images/Pythonnew/Python16.3.png)

Step 3) Loops over the days of a month by using c.itermonthday (2025,4), it will fetch the
total number of days for that month.
(//cdn.guru99.com/images/Pythonnew/Python16.4.png)

When you execute the code to get the total number of days for a specific month say
"April" then you will get 30 days in the output but you will also see some zeros along with
these days at the beginning and sometimes at the end of it.
Zeros in the output mean that the day of the week is in an overlapping month, which
means it does not belong to that month.
These zeros appears in output because, in your code you have mentioned day
(Thursday), so when you call function "c.itermonthdays", it will start counting days from
Thursday and your Thursday may not start with date 1st of April it might be 28th or 29th of
March, so when you execute the code it will start counting days from 28th of march and
any days after that till 1st of April. These days will be counted as zero and in the output
you will see these zeroes and same is applicable to the end of the month.
So except date 1-30 all the dates from previous as well as post month will appear in the
output as zeroes.

Step 4) You can fetch the data from the local system, like months or weekdays, etc
(//cdn.guru99.com/images/Pythonnew/Python16.5.png)

The output over here shows that we have printed out the months name from the local
system. Likewise, you can also fetch the weekdays name as shown below
The output will depend on the local system, suppose if your local system is some other
countries then it will give the output as per the local settings of that country. Here we
have months so it won't be a difference but if it is a week or day, it will certainly differ.
(//cdn.guru99.com/images/Pythonnew/Python16.6.png)

Step 5) You can fetch the list of the specific day for a whole year. For example, there is an
audit day on every first Monday of a week. You want to know the date of first Monday for
each month. You can use this code
(//cdn.guru99.com/images/Pythonnew/Python16.7.png)

mycal = calendar.monthcalendar(2025, month) will create calendar for the month


Set variables week1 and week2 to the First and second week of the calendar
Check if Week 1 contains Monday, set audit day
Else set audit day as the first Monday in week 2
The output shows the date for the first Monday that falls in that month.
The length of this Cal object is going to be a certain length, based on how many weeks
there in the month. In our case, it's going to be one or two as such first Monday of the
week will be in the first week most often but if not then consider the second week. Let
see in detail why we also consider the second week.
Here we are using the calendar's constant Monday, the calendar object gives you
constants that represent Sunday, Monday, Tuesday, so on, so forth. We've seen these
previously. So, if in week one the day represented by the Monday constant is not equal to
0, remember zeros means days that belong to another month. So, in this case, if it's zero,
it's going to be a Monday that belongs to the previous month. But if the first Monday is
not equal to 0, that means that my audit day will be within the week one. Otherwise, if
that is 0, then the first Monday isn't in the first week of the month, it's gotta be in the
second.
So, then I say okay, set my audit day variable to be the Monday represented by Week two.
So, audit day is going to come back with whatever the day is for either the first or second
week.

Here is the complete code

Python 2 Example
import calendar

# Create a plain text calendar

c = calendar.TextCalendar(calendar.THURSDAY)

str = c.formatmonth(2025, 1, 0, 0)

print str

# Create an HTML formatted calendar

hc = calendar.HTMLCalendar(calendar.THURSDAY)

str = hc.formatmonth(2025, 1)

print str

# loop over the days of a month

# zeroes indicate that the day of the week is in a next month or overlappi
ng month

for i in c.itermonthdays(2025, 4):

print i

# The calendar can give info based on local such a names of days and m
onths (full and abbreviated forms)

for name in calendar.month_name:

print name

for day in calendar.day_name:

print day

# calculate days based on a rule: For instance an audit day on the sec
ond Monday of every month

# Figure out what days that would be for each month, we can use the sc
ript as shown here

for month in range(1, 13):

# It retrieves a list of weeks that represent the month

mycal = calendar.monthcalendar(2025, month)

# The first MONDAY has to be within the first two weeks

week1 = mycal[0]

week2 = mycal[1]

if week1[calendar.MONDAY] != 0:

auditday = week1[calendar.MONDAY]

else:

# if the first MONDAY isn't in the first week, it must be in the s


econd week

auditday = week2[calendar.MONDAY]

print "%10s %2d" % (calendar.month_name[month], auditday)

Python 3 Example
import calendar

# Create a plain text calendar

c = calendar.TextCalendar(calendar.THURSDAY)

str = c.formatmonth(2025, 1, 0, 0)

print(str)

# Create an HTML formatted calendar

hc = calendar.HTMLCalendar(calendar.THURSDAY)

str = hc.formatmonth(2025, 1)

print(str)

# loop over the days of a month

# zeroes indicate that the day of the week is in a next month or overlappi
ng month

for i in c.itermonthdays(2025, 4):

print(i)

# The calendar can give info based on local such a names of days and m
onths (full and abbreviated forms)

for name in calendar.month_name:

print(name)

for day in calendar.day_name:

print(day)

# calculate days based on a rule: For instance an audit day on the sec
ond Monday of every month

# Figure out what days that would be for each month, we can use the sc
ript as shown here

for month in range(1, 13):

# It retrieves a list of weeks that represent the month

mycal = calendar.monthcalendar(2025, month)

# The first MONDAY has to be within the first two weeks

week1 = mycal[0]

week2 = mycal[1]

if week1[calendar.MONDAY] != 0:

auditday = week1[calendar.MONDAY]

else:

# if the first MONDAY isn't in the first week, it must be in the s


econd week

auditday = week2[calendar.MONDAY]

print("%10s %2d" % (calendar.month_name[month], auditday))

Summary:

In Python, you can format the calendar the way you want as you can change the day of
the month to begin
Print out the Calendar in HTML format
Fetch the data from the local system, like months or weekdays
Fetch the list of the specific day for a whole year
 Prev (/date-time-and-datetime-classes-in-python.html) Report a Bug

Next  (/pytest-tutorial.html)

You Might Like


Python round() function with EXAMPLES (/round-function-python.html)

Python range() Function: Float, List, For loop Examples (/python-range-function.html)

Python CALENDAR Tutorial with Example (/calendar-in-python.html)

Python map() function with EXAMPLES (/python-map-function.html)

PyTest Tutorial: What is, How to Install, Framework, Assertions (/pytest-tutorial.html)

 (https://www.facebook.com/guru99com/)
 (https://twitter.com/guru99com) 
(https://www.linkedin.com/company/guru99/)

(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)

(https://forms.aweber.com/form/46/724807646.htm)

About
About Us (/about-us.html)

Advertise with Us (/advertise-us.html)

Write For Us (/become-an-instructor.html)

Contact Us (/contact-us.html)

Career Suggestion
SAP Career Suggestion Tool (/best-sap-module.html)

Software Testing as a Career (/software-testing-career-


complete-guide.html)

Interesting
eBook (/ebook-pdf.html)

Blog (/blog/)

Quiz (/tests.html)

SAP eBook (/sap-ebook-pdf.html)

Execute online
Execute Java Online (/try-java-editor.html)

Execute Javascript (/execute-javascript-online.html)

Execute HTML (/execute-html-online.html)

Execute Python (/execute-python-online.html)

© Copyright - Guru99 2021

        Privacy Policy (/privacy-policy.html)  |  Affiliate


Disclaimer (/affiliate-earning-disclaimer.html)  |  ToS
(/terms-of-service.html)

You might also like