Python
11 Best Python Programming Books for Beginner (2021 Update)
Python is the de facto language for data scientists, statisticians, machine learning experts, and...
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.
Let's quickly change the value from Sunday to Thursday and check the output
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
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.
Step 4) You can fetch the data from the local system, like months or weekdays, etc
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
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 overlapping month
for i in c.itermonthdays(2025, 4):
print i
# The calendar can give info based on local such a names of days and months (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 second Monday of every month
# Figure out what days that would be for each month, we can use the script 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 second 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 overlapping month
for i in c.itermonthdays(2025, 4):
print(i)
# The calendar can give info based on local such a names of days and months (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 second Monday of every month
# Figure out what days that would be for each month, we can use the script 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 second week
auditday = week2[calendar.MONDAY]
print("%10s %2d" % (calendar.month_name[month], auditday))
Summary:
Python is the de facto language for data scientists, statisticians, machine learning experts, and...
The following best online Python courses will help you to learn Python programming from home....
What is Python? Python is an object-oriented programming language created by Guido Rossum in 1989....
Before we learn Django, let's understand: What is a Web Framework? A web framework is a code...
What is JSON? JSON is a standard format for data exchange, which is inspired by JavaScript....
What is C++? C++ is widely used in general-purpose programming languages. The language allows you...