Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is calendar module in python?
The calendar module in Python provides functions to display calendars and perform calendar-related operations. By default, these calendars have Monday as the first day of the week and Sunday as the last.
Display the Calendar of an Year
To display the calendar of an year, use the calendar() method and set year as the parameter ?
import calendar # Set the year year = 2022 # Display the calendar print(calendar.calendar(year))
The output of the above code is ?
2022
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 1 2 3 4 5 6
3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13
10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20
17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27
24 25 26 27 28 29 30 28 28 29 30 31
31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 1 2 3 4 5
4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12
11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19
18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26
25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30
30 31
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 2 3 4 5 6 7 1 2 3 4
4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11
11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18
18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25
25 26 27 28 29 30 31 29 30 31 26 27 28 29 30
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 1 2 3 4
3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11
10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18
17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25
24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31
31
Display a Specific Month
To display a specific month of a calendar, use the month() method with year and month as parameters ?
import calendar # Display August 2022 print(calendar.month(2022, 8))
The output of the above code is ?
August 2022
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Get Weekday Numbers
The calendar module provides constants for weekday numbers, where Monday is 0 and Sunday is 6 ?
import calendar
print(f'Monday Weekday Number: {calendar.MONDAY}')
print(f'Tuesday Weekday Number: {calendar.TUESDAY}')
print(f'Wednesday Weekday Number: {calendar.WEDNESDAY}')
print(f'Thursday Weekday Number: {calendar.THURSDAY}')
print(f'Friday Weekday Number: {calendar.FRIDAY}')
print(f'Saturday Weekday Number: {calendar.SATURDAY}')
print(f'Sunday Weekday Number: {calendar.SUNDAY}')
The output of the above code is ?
Monday Weekday Number: 0 Tuesday Weekday Number: 1 Wednesday Weekday Number: 2 Thursday Weekday Number: 3 Friday Weekday Number: 4 Saturday Weekday Number: 5 Sunday Weekday Number: 6
Check if a Year is a Leap Year
To check if a year is a leap year, use the isleap() method. It returns True for leap years and False otherwise ?
import calendar # Check if any of the years is a leap year print(calendar.isleap(2020)) print(calendar.isleap(2021)) print(calendar.isleap(2022)) print(calendar.isleap(2023)) print(calendar.isleap(2024)) print(calendar.isleap(2025))
The output of the above code is ?
True False False False True False
Get Day of the Week for a Date
Use weekday() to find which day of the week a specific date falls on ?
import calendar
# Get day of week for January 1, 2024 (Monday = 0)
day = calendar.weekday(2024, 1, 1)
print(f'January 1, 2024 is weekday: {day}')
# Get month calendar with specific first day
print(calendar.monthcalendar(2024, 1))
The output of the above code is ?
January 1, 2024 is weekday: 0 [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31, 0, 0, 0, 0]]
Conclusion
The calendar module provides essential functions for calendar operations including displaying full-year or monthly calendars, checking leap years, and determining weekdays for specific dates. These utilities are particularly useful for date-related applications and scheduling systems.
