Python Kalendermodule met voorbeeld
⚡ Slimme samenvatting
The calendar module in Python generates text and HTML calendars using the TextCalendar and HTMLCalendar classes. It performs date calculations based on the day, month, and year, and lets you customize the starting weekday for any month.

De calendar module in Python has a calendar class that allows calculations for various tasks based on the date, month, and year. On top of that, the TextCalendar en HTMLCalendar lessen in Python let you edit the calendar and use it as per your requirement. Let us see what we can do with the Python kalender.
What is the calendar Module in Python?
De calendar module deel van de Python standard library, so it is available without any installation. It groups several classes and helper functions that build calendars and answer common date questions, such as which weekday a date falls on or how many days a month contains.
The module provides five main classes:
- Agenda is a base class that supplies the data the other calendars are built from.
- TextCalendar produces plain-text calendars for a month or a full year.
- HTMLCalendar produces calendars as HTML tables for web pages.
- LocaleTextCalendar is a text calendar that uses the day and month names of a chosen locale.
- LocaleHTMLCalendar is the HTML equivalent that respects a chosen locale.
By default, a calendar week begins on Monday, following the European convention. You can change the first day using a constant such as calendar.SUNDAY, as shown in the examples below.
How to Print a Text Calendar in Python
Stap 1) Voer de code uit.
- 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. The start of the month will be Sunday. In Python, you can format the calendar as you like and change the day the month begins with.
- Code Line # 4: str= c.formatmonth(2025,1) creates a calendar for the year 2025, Month 1 – January.
- Code Regel 5: print str zal de uitvoer afdrukken.
Let us quickly change the value from Sunday to Thursday and check the output.
How to Print an HTML Calendar in Python
Stap 2) You can also print the calendar in HTML format. This feature is helpful for developers who want to make changes to the look and feel of the calendar.
How to Loop Over the Days of a Month
Stap 3) Passanten over the days of a month by using c.itermonthdays (2025,4); it will fetch the total number of days for that month.
- When you execute the code to get the total number of days for a specific month, say “April”, 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.
- Nullen in de uitvoer betekenen dat de dag van de week zich in een overlap bevindt.ping maand, wat betekent dat het niet bij die maand hoort.
- These zeros appear in the output because, in your code, you have mentioned a day (Thursday). So when you call the function “c.itermonthdays”, it starts counting days from Thursday, and your Thursday may not start on the 1st of April – it might be the 28th or 29th of March. So the code starts counting days from the 28th of March and any days after that until the 1st of April. These days are counted as zeros, and in the output you will see these zeros. The same applies to the end of the month.
- So, except for the dates 1–30, all the dates from the previous as well as the following month appear in the output as zeros.
How to Fetch Month and Weekday Names From the Local System
Stap 4) You can fetch data from the local system, such as months or weekdays.
- The output here shows that we have printed the month names from the local system. Likewise, you can also fetch the weekday names, as shown below.
- The output depends on the local system. If your local system is set to another country, it gives the output as per the local settings of that country. Here we have months, so it will not make a difference, but for a week or a day, it will certainly differ.
How to Find a Specific Day for the Whole Year
Stap 5) You can fetch the list of a specific day for a whole year. For example, there is an audit day on every first Monday of a week. Suppose you want to know the date of the first Monday for each month. You can use this code.
- mycal = calendar.monthcalendar(2025, month) creates a calendar for the month.
- Set the variables week1 and week2 to the first and second week of the calendar.
- Check whether week 1 contains Monday and set the audit day.
- Otherwise, set the audit day as the first Monday in week 2.
- De uitvoer toont de datum voor de eerste Monday dat valt in die maand.
- The length of this Cal object depends on how many weeks there are in the month. In our case, it is one or two, as the first Monday of the week is in the first week most often, but if not, then we consider the second week. Let us see in detail why we also consider the second week.
- Here we are using the calendar constant Monday. The calendar object gives you constants that represent Sunday, Monday, Tuesday, and so on. If, in week one, the day represented by the Monday constant is not equal to 0 – remember, zeros mean days that belong to another month – then that Monday belongs to this month and the audit day is within week one. If it is 0, then the first Monday is not in the first week of the month, so it must be in the second.
- In that case, we set the audit day variable to the Monday represented by week two. The audit day comes back with whatever the day is for either the first or the second week.
Volledige Python Agenda Code Voorbeeld
Here is the complete code.
Python 2 Voorbeeld
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 Voorbeeld
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))
Useful Functions in the Python calendar Module
Besides the walkthrough above, the calendar module ships with several handy functions that work without creating a class instance:
- calendar.calendar(year) returns a full-year calendar as a multi-line string, while calendar.prcal(year) prints it directly.
- calendar.month(year, month) returns a single month as text, and calendar.prmonth(year, month) drukt het af.
- calendar.monthcalendar(year, month) returns the month as a list of week rows, where each day is an integer and days outside the month are 0.
These functions make quick, one-line calendar output possible, which is convenient in scripts and interactive sessions where you do not need the extra control of the TextCalendar or HTMLCalendar classes.
How to Check for a Leap Year in Python
The calendar module can also tell you whether a year is a leap year, which matters because February has 29 days instead of 28 in such years. Two functions handle this:
- calendar.isleap(year) returns True when the year is a leap year and False otherwise.
- calendar.leapdays(y1, y2) returns the number of leap years in the range from y1 up to, but not including, y2.
For example, calendar.isleap(2024) returns True, while calendar.isleap(2025) returns False. Leap-year checks are useful whenever you validate dates, schedule recurring events, or count the exact number of days between two years.







