Python Calendar monthrange Function

The monthrange function in Python's calendar module returns information about the specified month and year, including the starting day of the week and the number of days in the month. This function is useful for generating calendars and planning events.

Table of Contents

  1. Introduction
  2. monthrange Function Syntax
  3. Examples
    • Basic Usage
    • Determining the Start Day and Number of Days in a Month
  4. Real-World Use Case
  5. Conclusion

Introduction

The monthrange function in Python's calendar module provides the starting day of the week and the number of days in a specified month and year. The function returns a tuple where the first value represents the starting day of the week (0=Monday, 6=Sunday), and the second value represents the number of days in the month.

monthrange Function Syntax

Here is how you use the monthrange function:

import calendar

start_day, num_days = calendar.monthrange(year, month)

Parameters:

  • year: The year as an integer.
  • month: The month as an integer (1-12).

Returns:

  • A tuple (start_day, num_days) where:
    • start_day is an integer representing the day of the week the month starts on (0=Monday, 6=Sunday).
    • num_days is an integer representing the number of days in the month.

Examples

Basic Usage

Here is an example of how to use the monthrange function to get the starting day and the number of days in a given month.

Example

import calendar

# Getting the start day and number of days for July 2024
year = 2024
month = 7
start_day, num_days = calendar.monthrange(year, month)
print(f"July 2024 starts on day {start_day} and has {num_days} days.")

Output:

July 2024 starts on day 0 and has 31 days.

Determining the Start Day and Number of Days in a Month

This example demonstrates how to get the starting day and number of days for another specific month.

Example

import calendar

# Getting the start day and number of days for February 2023
year = 2023
month = 2
start_day, num_days = calendar.monthrange(year, month)
print(f"February 2023 starts on day {start_day} and has {num_days} days.")

Output:

February 2023 starts on day 2 and has 28 days.

Real-World Use Case

Generating Monthly Calendars

In real-world applications, the monthrange function can be used to generate monthly calendars by knowing the start day and the number of days in a month.

Example

import calendar

def generate_month_calendar(year, month):
    start_day, num_days = calendar.monthrange(year, month)
    print(f"{calendar.month_name[month]} {year}")
    print("Mo Tu We Th Fr Sa Su")
    
    # Print leading spaces for the first week
    print("   " * start_day, end="")
    
    for day in range(1, num_days + 1):
        print(f"{day:2} ", end="")
        if (start_day + day) % 7 == 0:
            print()  # New line after Sunday

    print()

# Example usage
generate_month_calendar(2024, 7)

Output:

July 2024
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 

Conclusion

The monthrange function in Python's calendar module provides the starting day of the week and the number of days in a specified month and year. This function is useful for generating calendars and planning events by giving you the necessary information about the structure of any month.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare