PY0015 Python CALENDAR Tutorial with Example
Posted by Superadmin on November 10 2018 13:47:40

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.

Python CALENDAR Tutorial with Example

Let's quickly change the value from Sunday to Thursday and check the output

Python CALENDAR Tutorial with Example

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

Python CALENDAR Tutorial with Example

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.

Python CALENDAR Tutorial with Example

Step 4) You can fetch the data from the local system, like months or weekdays, etc

Python CALENDAR Tutorial with Example

Python CALENDAR Tutorial with Example

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, and want to know the date for each month of the year, you can use this code

Python CALENDAR Tutorial with Example

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 second MONDAY has to be within the first two weeks
        week1 = mycal[1]
        week2 = mycal[2]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
             # if the second 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 second MONDAY has to be within the first two weeks
        week1 = mycal[1]
        week2 = mycal[2]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
             # if the second 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: