How to Get Previous, Current and Next Day System Dates in Python

In this tutorial, you will learn how to get the current system date and how to basic date arithmetic in Python.

Get the Current Date and Time

The following Python script prints the current date and timestamp according to your system/computer:

now.py
import datetime

now = datetime.datetime.today()

print(f"Current Date and Time: {now}")

The output should look something like this:

Current Date and Time: 2023-05-22 22:47:16.879358

Get Just the Current Date

Use the strftime method to format your dates:

today.py
import datetime

today = datetime.datetime.today().strftime('%Y-%m-%d')

print(f"Current Date: {today}")

where %Y represents the year, %m the month, and %d the day of the month. Rearrange them to your liking.

The output should look something like this:

Current Date: 2023-05-22

Get the Previous and Next Day Dates

Use timedelta function to calculate the previous and next day dates:

import datetime

previous_day = datetime.datetime.today() - datetime.timedelta(days=1)
previous_day = previous_day.strftime('%Y-%m-%d')
print(f"Previous Date: {previous_day}")

next_day = datetime.datetime.today() + datetime.timedelta(days=1)
next_day = next_day.strftime('%Y-%m-%d')
print(f"Next Date: {next_day}")

The output should look something like this:

Previous Date: 2023-05-21
Next Date: 2023-05-23

That's it! You just learned how to get the current, previous and next day dates!