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

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

To start, here is the syntax that you may use to get the system dates with the timestamps (you’ll later see how to get the dates without the timestamps):

Current Date:

import datetime
Current_Date = datetime.datetime.today()
print (Current_Date)

Previous Date:

import datetime
Previous_Date = datetime.datetime.today() - datetime.timedelta(days=1)
print (Previous_Date)

Next-Day Date:

import datetime
NextDay_Date = datetime.datetime.today() + datetime.timedelta(days=1)
print (NextDay_Date)

Next, you’ll see the following two cases:

  1. Non-Formatted System Dates in Python (including the timestamps)
  2. Formatted System Dates in Python (without the timestamps)

Case 1: 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 (including the timestamps) in Python:

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

Here is the full Python code that you may use:

import datetime

Current_Date = datetime.datetime.today()
print ('Current Date: ' + str(Current_Date))

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

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

Notice that in order to get the previous date, you’ll need to subtract 1 day from the current date:

datetime.datetime.today()  datetime.timedelta(days=1)

If, for example, you want to get the day before yesterday, you may then deduct 2 days as follows:

datetime.datetime.today()  datetime.timedelta(days=2)

To get future dates, simply use the plus symbol, and set your desired number of days (in our example, we added 1 day into the future):

datetime.datetime.today() + datetime.timedelta(days=1)

Here are the results if someone ran the Python code on 22-Mar-2021:

Current Date: 2021-03-22 13:55:20.791683
Previous Date: 2021-03-21 13:55:20.791683
Next Date: 2021-03-23 13:55:20.791683

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

Case 2: 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 strftime 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 = datetime.datetime.today().strftime ('%d%m%Y') # format the date to ddmmyyyy
print ('Current Date: ' + str(Current_Date_Formatted))
 
Previous_Date = datetime.datetime.today() - 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.datetime.today() + 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))

On 22-Mar-2021, the results were:

Current Date: 22032021
Previous Date: 21032021
Next Date: 23032021

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 instance, 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.