Get the Previous Month or Day in Python

This article will discuss best practices for parsing dates and getting a specific month, day, or year in the past or the future.

We will use different Python libraries to manipulate dates and times. Apart from the standard datetime package (documentation), we will leverage other modules like dateparser and dateutil to extract localized dates in strings.

For example, the packages should be able to parse strings like “20 days ago”, “two months and two days ago”, “yesterday”, “445 days ago at noon”, et cetera.

Getting the first and the last day of any month

In this case, the datetime package can do the job. All we need to do is get the date today and then set the day to 1.


2022-05-01 09:11:00.894081

If you are only interested in the date and not the time, you can call in line two, then set the day value to 1.


<datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, …)>

<, month, day)>

We can get the last day of the month using an inbuilt package called calendar in Python. Precisely, we will make use of the monthrange() function. This function takes the year and month and returns a tuple of the two values – the weekday of the first day of the given month and the number of days in the month. The weekday is integer coded from 0 being a Monday through 6 being Sunday.


<weekday_first_day, number_of_days = calendar.monthrange(year, month)>


(3, 29)
(6, 31)

From this example, we can note that February 2024 has 29 days (as expected, being a leap year) and that the first day of the month is 3 (Thursday).

We can then use the calendar.monthrange() function to get the last day of the month as follows:



Note that we have just borrowed the ideas we already know from how we got the month’s first day and used the calendar.monthrange() function to pick the number of days in a month for the last day of the month.

Getting the last day of the previous month

Since we already know how to get the first day of the month, we can use the timedelta() function to subtract one day from that.

<datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)>



Getting the day of the week for a given date

Let us discuss two approaches here:

Approach #1 Using date.strftime(format)

The function strftime() returns a string representation of the given date. An explicit format string controls it. To get the weekday, we will use the %A directive as follows (For a complete list of formatting directives, you can read the documentation of strftime)



Today is Tuesday, based on the first line and November 11, 2021, was on Sunday.

Approach #2 Using date.weekday()

This function returns the day of the week as an integer, where Monday is 0 and Sunday is 6. We can proceed to convert the integer into the full name accordingly if we choose to. We can use a dictionary to do this conversion or calendar.day_name[].




Smart Dates

This section will discuss the parsing of localized dates found in string formats that may not fit the string format required by datetime.strftime(), which we discussed in the previous section.

We will be parsing relative dates like “tomorrow”, “in 20 days”, “2 years and 2 weeks ago”, “yesterday”, etc. Let’s now discuss the two packages we can use to parse relative dates

Used case: Parsing of relative dates is crucial when dealing with dates that have been recorded in different formats.

Parsing relative dates using the dateparser library

The parse() function in dateparser can parse relative dates by factoring features like time zones, language, lookup dates in long strings, and even supporting different calendar systems. The general syntax for the parser is


<dateparser.parse(date_string, date_formats=None, languages=None, locales=None, region=None, settings=None, detect_languages_function=None)>


Parsing dates with dateutil package

Like dateparser.parser(), parse() function in dateutil is used to parse dates to remove ambiguity in date formats in the dataset. Here are some examples of what dateutil can do

That is just an intro to what this beautiful package can do. You can read more about the different uses of dateutil in the documentation.