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

In this short post, I’m going to show you how to get the previous, current and next-day system dates in Python. I’ll also demonstrate how you can 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

The code below contains 4 main elements in order to get the system dates in Python:

  1. Import of the datetime module
  2. Current_date = datetime.datetime.today()
  3. Previous_Date = datetime.datetime.today() – datetime.timedelta(days=1)
  4. NextDay_Date = datetime.datetime.today() + datetime.timedelta(days=1)

Here is the full Python code:

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, we subtracted 1 day from the current date by using:

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

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

datetime.datetime.today()  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.datetime.today() + datetime.timedelta(days=1)

 

Let’s suppose that the date we ran the Python code was 20-Mar-2018. Here are the results that you’ll get after running the code:

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 different formats. 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:

import datetime
 
Current_Date_Formatted = datetime.datetime.today().strftime ('%d%m%Y') # format the date to ddmmyyyyprint (Current_Date)
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))

 

And if you ran the above code for the date of 20-Mar-2018, you’ll get 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 can 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.