Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to get the last day of month in Python

To get the last day of a month in Python, you can use the calendar.monthrange() function from the calendar module in the Python standard library. The calendar.monthrange() function takes two arguments, the year and the month, and returns a tuple containing the weekday of the first day of the month and the number of days in the month.

Here's an example:

import calendar

# The input year and month
year = 2023
month = 5

# Get the number of days in the input month
_, last_day = calendar.monthrange(year, month)

# Print the last day of the month
print("The last day of the month is:", last_day)

In this example, we have an input year and month called year and month. We use the calendar.monthrange() function to get the number of days in the input month. The function returns a tuple, and we assign the second element of the tuple to the last_day variable. Finally, we print the last day of the month.

If you want to get the date object representing the last day of the month, you can use the datetime.date() constructor:

import calendar
import datetime

# The input year and month
year = 2023
month = 5

# Get the number of days in the input month
_, last_day = calendar.monthrange(year, month)

# Create a date object representing the last day of the month
last_day_date = datetime.date(year, month, last_day)

# Print the last day of the month as a date object
print("The last day of the month is:", last_day_date)

In this example, we use the datetime.date() constructor to create a date object representing the last day of the month, and we print the date object.

  1. Python calendar.monthrange() for the last day of the month:

    import calendar
    
    year = 2023
    month = 12
    
    _, last_day = calendar.monthrange(year, month)
    
    print(f"Last day of the month: {last_day}")
    
  2. Get the last day of the month with the datetime module in Python:

    from datetime import datetime, timedelta
    
    year = 2023
    month = 12
    
    first_day_of_next_month = datetime(year, month + 1, 1)
    last_day = (first_day_of_next_month - timedelta(days=1)).day
    
    print(f"Last day of the month: {last_day}")
    
  3. Calculating the last day of the month using pandas in Python:

    import pandas as pd
    
    year = 2023
    month = 12
    
    last_day = pd.Timestamp(f"{year}-{month + 1}-01") - pd.DateOffset(days=1)
    
    print(f"Last day of the month: {last_day.day}")
    
  4. Using calendar.monthcalendar() for the last day of the month in Python:

    import calendar
    
    year = 2023
    month = 12
    
    month_calendar = calendar.monthcalendar(year, month)
    last_week = month_calendar[-1]
    last_day = max(day for day in last_week if day != 0)
    
    print(f"Last day of the month: {last_day}")
    
  5. Get the last day of the month with calendar.monthlen() in Python:

    import calendar
    
    year = 2023
    month = 12
    
    last_day = calendar.monthlen(year, month)
    
    print(f"Last day of the month: {last_day}")
    
  6. Finding the end of the month with dateutil.relativedelta in Python:

    from datetime import datetime
    from dateutil.relativedelta import relativedelta
    
    year = 2023
    month = 12
    
    last_day = (datetime(year, month, 1) + relativedelta(months=1, days=-1)).day
    
    print(f"Last day of the month: {last_day}")
    
  7. Python pandas for the last day of the month with date_range():

    import pandas as pd
    
    year = 2023
    month = 12
    
    last_day = pd.date_range(start=f"{year}-{month}-01", periods=2, freq='M')[1].day
    
    print(f"Last day of the month: {last_day}")
    
  8. Retrieve the last day of the month with timedelta in Python:

    from datetime import datetime, timedelta
    
    year = 2023
    month = 12
    
    first_day_of_next_month = datetime(year, month + 1, 1)
    last_day = (first_day_of_next_month - timedelta(days=1)).day
    
    print(f"Last day of the month: {last_day}")
    
  9. Calculating the end of the month with calendar.isleap() in Python:

    import calendar
    
    year = 2023
    month = 12
    
    _, last_day = calendar.monthrange(year, month)
    last_day += calendar.isleap(year) and month == 2
    
    print(f"Last day of the month: {last_day}")
    
  10. Get the last day of the month using calendar.day_name() in Python:

    import calendar
    
    year = 2023
    month = 12
    
    _, last_day = calendar.monthrange(year, month)
    day_name = calendar.day_name[calendar.weekday(year, month, last_day)]
    
    print(f"Last day of the month ({day_name}): {last_day}")
    
  11. Finding the last day of the month with monthdelta in Python:

    from datetime import datetime
    from monthdelta import monthdelta
    
    year = 2023
    month = 12
    
    last_day = (datetime(year, month, 1) + monthdelta(1) - timedelta(days=1)).day
    
    print(f"Last day of the month: {last_day}")
    
  12. Python numpy for the last day of the month with datetime64:

    import numpy as np
    
    year = 2023
    month = 12
    
    last_day = np.datetime64(f'{year}-{month + 1:02}-01') - np.timedelta64(1, 'D')
    
    print(f"Last day of the month: {last_day}")
    
  13. Using calendar.isleap() and calendar.month_abbr() for the last day in Python:

    import calendar
    
    year = 2023
    month = 12
    
    _, last_day = calendar.monthrange(year, month)
    last_day += calendar.isleap(year) and month == 2
    
    month_name = calendar.month_abbr[month]
    
    print(f"Last day of {month_name}, {year}: {last_day}")
    
  14. Python arrow library for the last day of the month:

    import arrow
    
    year = 2023
    month = 12
    
    last_day = arrow.get(f'{year}-{month + 1:02}-01').shift(days=-1).day
    
    print(f"Last day of the month: {last_day}")
    
  15. Retrieve the last day of the month using calendar.monthcalendar() in Python:

    import calendar
    
    year = 2023
    month = 12
    
    month_calendar = calendar.monthcalendar(year, month)
    last_week = month_calendar[-1]
    last_day = max(day for day in last_week if day != 0)
    
    print(f"Last day of the month: {last_day}")
    
  16. Finding the end of the month with calendar.monthrange() and timedelta in Python:

    from datetime import datetime, timedelta
    import calendar
    
    year = 2023
    month = 12
    
    _, last_day = calendar.monthrange(year, month)
    last_day = datetime(year, month, last_day) + timedelta(days=1, seconds=-1)
    
    print(f"Last day of the month: {last_day}")
    
  17. Get the last day of the month with calendar.monthcalendar() and max() in Python:

    import calendar
    
    year = 2023
    month = 12
    
    month_calendar = calendar.monthcalendar(year, month)
    last_day = max(max(week) for week in month_calendar)
    
    print(f"Last day of the month: {last_day}")
    
  18. Calculating the last day of the month using the pendulum library in Python:

    import pendulum
    
    year = 2023
    month = 12
    
    last_day = pendulum.datetime(year, month, 1).end_of('month').day
    
    print(f"Last day of the month: {last_day}")