Calendar Functions in Python -(monthrange(), prcal(), weekday()?)

Python's calendar module provides useful functions for working with dates and calendars. We'll explore three key methods: monthrange(), prcal(), and weekday() for date calculations and calendar display.

calendar.monthrange(year, month)

The monthrange() method returns a tuple containing the starting weekday number (0=Monday, 6=Sunday) and the number of days in the specified month ?

Example

import calendar

# Getting month information for January 2019
year = 2019
month = 1

weekday, no_of_days = calendar.monthrange(year, month)
print(f'First weekday of the month: {weekday}')
print(f'Number of days: {no_of_days}')

# Let's also check what weekday 1 means
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
print(f'January 1, 2019 starts on: {weekdays[weekday]}')
First weekday of the month: 1
Number of days: 31
January 1, 2019 starts on: Tuesday

calendar.prcal(year)

The prcal() method prints the entire year's calendar directly to the console without requiring a print statement ?

Example

import calendar

# Print calendar for 2024
year = 2024
calendar.prcal(year)
                                  2024

      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  3  4  5  6  7                1  2  3  4                   1  2  3
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       4  5  6  7  8  9 10
15 16 17 18 19 20 21      12 13 14 15 16 17 18      11 12 13 14 15 16 17
22 23 24 25 26 27 28      19 20 21 22 23 24 25      18 19 20 21 22 23 24
29 30 31                  26 27 28 29               25 26 27 28 29 30 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  4  5  6  7             1  2  3  4  5                      1  2
 8  9 10 11 12 13 14       6  7  8  9 10 11 12       3  4  5  6  7  8  9
15 16 17 18 19 20 21      13 14 15 16 17 18 19      10 11 12 13 14 15 16
22 23 24 25 26 27 28      20 21 22 23 24 25 26      17 18 19 20 21 22 23
29 30                     27 28 29 30 31            24 25 26 27 28 29 30

        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  4  5  6  7                1  2  3  4                         1
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       2  3  4  5  6  7  8
15 16 17 18 19 20 21      12 13 14 15 16 17 18       9 10 11 12 13 14 15
22 23 24 25 26 27 28      19 20 21 22 23 24 25      16 17 18 19 20 21 22
29 30 31                  26 27 28 29 30 31         23 24 25 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  3  4  5  6                   1  2  3                         1
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       2  3  4  5  6  7  8
14 15 16 17 18 19 20      11 12 13 14 15 16 17       9 10 11 12 13 14 15
21 22 23 24 25 26 27      18 19 20 21 22 23 24      16 17 18 19 20 21 22
28 29 30 31               25 26 27 28 29 30         23 24 25 26 27 28 29
                                                    30 31

calendar.weekday(year, month, day)

The weekday() method returns the weekday number (0=Monday, 6=Sunday) for a specific date ?

Example

import calendar

# Check what day January 28, 2020 was
year = 2020
month = 1
day = 28

weekday_num = calendar.weekday(year, month, day)
print(f'Weekday number: {weekday_num}')

# Convert to readable day name
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
print(f'January 28, 2020 was a {weekdays[weekday_num]}')

# Check multiple dates
dates = [(2024, 12, 25), (2024, 7, 4), (2024, 1, 1)]
for year, month, day in dates:
    day_name = weekdays[calendar.weekday(year, month, day)]
    print(f'{year}-{month:02d}-{day:02d} is a {day_name}')
Weekday number: 1
January 28, 2020 was a Tuesday
2024-12-25 is a Wednesday
2024-07-04 is a Thursday
2024-01-01 is a Monday

Practical Usage

These functions are commonly used together for date calculations ?

import calendar

def analyze_month(year, month):
    # Get month info
    first_weekday, days_in_month = calendar.monthrange(year, month)
    
    # Get last day weekday
    last_weekday = calendar.weekday(year, month, days_in_month)
    
    weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    
    print(f'Month {month}/{year}:')
    print(f'- Starts on: {weekdays[first_weekday]}')
    print(f'- Ends on: {weekdays[last_weekday]}')
    print(f'- Has {days_in_month} days')

analyze_month(2024, 2)  # February 2024 (leap year)
Month 2/2024:
- Starts on: Thursday
- Ends on: Thursday
- Has 29 days

Conclusion

Python's calendar module provides essential functions for date calculations. Use monthrange() to get month details, prcal() to display yearly calendars, and weekday() to determine specific weekdays.

Updated on: 2026-03-25T07:05:48+05:30

657 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements