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

To get the system dates with the timestamps in Python (check case 2 below 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

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

print(next_day_date)

Case 1: System Dates with the Timestamps

Here is the Python code to get the current, previous and next-day dates with the timestamp:

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))

next_day_date = datetime.datetime.today() + datetime.timedelta(days=1)
print("Next Date: " + str(next_day_date))

The result (taken on 25-April-2024):

Current Date: 2024-04-25 13:25:41.209261
Previous Date: 2024-04-24 13:25:41.209261
Next Date: 2024-04-26 13:25:41.209261

Case 2: Formatted System Dates without the Timestamps

What if you want to present your system dates using a different format?

For example, to present the system dates as ddmmyyyy (without the timestamps), 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:

import datetime

current_date = datetime.datetime.today().strftime("%d%m%Y")
print("Current Date: " + str(current_date))

previous_date = (datetime.datetime.today() - datetime.timedelta(days=1)).strftime("%d%m%Y")
print("Previous Date: " + str(previous_date))

next_day_date = (datetime.datetime.today() + datetime.timedelta(days=1)).strftime("%d%m%Y")
print("Next Date: " + str(next_day_date))

The result (taken on 25-April-2024):

Current Date: 25042024
Previous Date: 24042024
Next Date: 26042024

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 months in characters, rather than in digits, you may then replace the %m with %b within the strftime brackets:

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

The complete code:

import datetime

current_date = datetime.datetime.today().strftime("%d-%b-%Y")
print("Current Date: " + str(current_date))

previous_date = (datetime.datetime.today() - datetime.timedelta(days=1)).strftime("%d-%b-%Y")
print("Previous Date: " + str(previous_date))

next_day_date = (datetime.datetime.today() + datetime.timedelta(days=1)).strftime("%d-%b-%Y")
print("Next Date: " + str(next_day_date))

The result (taken on 25-April-2024):

Current Date: 25-Apr-2024
Previous Date: 24-Apr-2024
Next Date: 26-Apr-2024

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