Get Previous, Current and Next-Day System Dates in Python

In this short post, I’ll show you how to get the previous, current and next-day system dates in Python. I’ll also explain how to modify the Python code to get your desired date format.

To start, let’s look at the first case of getting the non-formatted system dates (including the timestamps) in Python.

Non-Formatted System Dates in Python

You can add the following 3 parts to the code in order to get the non-formatted system dates in Python:

  1. Current_date =
  2. Previous_Date = – datetime.timedelta(days=1)
  3. NextDay_Date = + datetime.timedelta(days=1)

Here is the full Python code that you may use:

import datetime

Current_Date =
print ('Current Date: ' + str(Current_Date))

Previous_Date = - datetime.timedelta(days=1)
print ('Previous Date: ' + str(Previous_Date))

NextDay_Date = + datetime.timedelta(days=1)
print ('Next Date: ' + str(NextDay_Date))

Notice that in order to get the previous date, we subtracted 1 day from the current date by using:  datetime.timedelta(days=1)

If, for example, your goal is to get the day before yesterday, you may then deduct 2 days as follows:  datetime.timedelta(days=2)

You can subtract as many days as you want based on your needs.


To get future dates, simply use the plus symbol, and set your desired number of days (in our example, we used 1 day into the future): + datetime.timedelta(days=1)


This is the result that I got when I ran the Python code on 13-May-2019:

Get Previous, Current and Next-Day System Dates in Python

You’ll notice that the results generated in Python include both the dates and the timestamps. In the next section, I’ll show you how to obtain the formatted system dates in Python (excluding the timestamps).

Formatted System Dates in Python

Let’s say that you want to present your system dates using a different format. For example, you may wish to present the system dates as DDMMYYYY (without the timestamps).

To accomplish this task, you’ll need to use the strftime command and set the format to %d%m%Y where:

  • %d represents the days of the month; and
  • %m represents the month; and
  • %Y represents the year

Here is the full Python code for our example:

import datetime
Current_Date_Formatted = ('%d%m%Y') # format the date to ddmmyyyy
print ('Current Date: ' + str(Current_Date_Formatted))
Previous_Date = - datetime.timedelta(days=1)
Previous_Date_Formatted = Previous_Date.strftime ('%d%m%Y') # format the date to ddmmyyyy
print ('Previous Date: ' + str(Previous_Date_Formatted))
NextDay_Date = + datetime.timedelta(days=1)
NextDay_Date_Formatted = NextDay_Date.strftime ('%d%m%Y') # format the date to ddmmyyyy
print ('Next Date: ' + str(NextDay_Date_Formatted))

When I ran the above code on 13-May-2019, I got the following results:

Get system dates in Python

This was just one example of the date format that you can use. You can apply different formats by changing the information within the strftime brackets.

For example, if you want to present the month in characters, rather than in digits, you may then replace the %m with %b within the strftime brackets:

strftime ('%d-%b-%Y')

You can check the Python strftime reference for a list of the formats that you can apply.

For additional guides about Python, please visit our Python tutorials.