Get last day of month in Python

Last day of month in Python

In this article, we will discuss how to find the last day of the month in Python using different modules.

Using the calendar.monthrange() function

The monthrange() function is used to return a tuple containing the first weekday of the month and the total number of days in the month. We have to pass the year and month to this function.

After getting the above mentioned values, we will create a new date object using the given date and use the replace() function to replace the day number with the total number of days in that month.

For example,

Output:

2021-05-31

For Python 3.7 and above, we can also use the monthlen() function from this module which returns the total number of days in a month.

Using the dateutil.relativedelta() function

The dateutil can be considered as an extension to the datetime module. Both these modules are very often used together and the dateutil module provides a vast range of functionalities to operate on datetime objects. We can use the relativedelta() function to compute the last date of the month from a given date.

For example,

Output:

2021-05-31

The above code gives the last date of the month in a datetime object.

Using the datetime module

We can create our own little function to return the last day of the month using the datetime module. This method can be considered unconventional but provides the desired result. Basically, we calculate the first day of the next month from a given date, then we subtract 1 day from this date and we get the date of the last day of the month.

The following code implements the above method.

Output:

2021-05-31

0 here indicates that the day is Monday.

Using the pandas.end_time() function

The pandas module deals very frequently with data containing dates and time values. That is why it is equipped with different functions that can work with such values. It is not advisable to use methods from this module unless one is working with DataFrames.

The end_time() function can return the final date and time of a given period. We can specify the date and set frequency to ‘M’, and we will get the final date of the month.

For example,

Output:

2021-05-31

Using the pandas.date_range()

The date_range() function is used to produce an object containing dates from the start to end of the specified values. We can modify this function to get the last day of the month.

See the following code.

Output:

2021-05-31 00:00:00

That’s all about how to get last day of month in Python.

Related Posts

  • Epoch to datetime in Python
    21 August

    Convert epoch to datetime in python

    Table of ContentsUsing the datetime.fromtimestamp() function to convert epoch to datetimeUsing the pandas.to_datetime() function to convert epoch to datetime in Python Epoch time refers to the time elapsed since time was started measuring on January 1, 1970. Usually, it is measured in seconds. It is also known as UNIX time. In this article, we will […]

  • Convert datetime to date in Python
    20 June

    Convert datetime to date in Python

    Table of ContentsUsing the date() function to convert datetime to date in Python.Using the dt.date function in pandas to convert datetime to date in Python. Date and time in Python are not a pre-defined data type itself. The datetime module can be imported to the Python code and it provides classes used that manipulate and […]

  • Convert unix timestamp to datetime in Python
    21 May

    Convert Unix timestamp to DateTime in python

    Table of ContentsUsing fromtimestamp() FunctionUsing strftime() FunctionUsing to_datetime() Function A UNIX timestamp is defined as the several numbers of seconds between a particular date and the Epoch i.e January 1, 1970 at UTC. In this tutorial, we will see some methods by which we can convert a UNIX timestamp to DateTime in Python. Using fromtimestamp() […]

  • Add days to date in Python
    19 May

    Add days to date in Python

    Table of ContentsUsing datetime.timedelta() FunctionAdd one day to date using datetime.timedelta()Using timedelta method from datetime moduleUsing Pandas Module In python, we deal with a lot of date and time-related problems. There is no specific data type for date and time still there is a lot of manipulation done on them in python. We can perform […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.