Home » Find Yesterday’s, Today’s and Tomorrow’s date in Python

Find Yesterday’s, Today’s and Tomorrow’s date in Python

Spring Framework Basics Video Course
1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
Oracle Java Certification

In this article we will show you how to find yesterdays date, todays date and tomorrow’s date, the current time is determined, and a method is used  that helps find previous day and next day’s dates.

Table of Contents

Example

First we import the necessary modules

Then we get todays date using datetime.now()

We then use timedelta to get tomorrows date date and the previous days date.

We then print all of the dates

# import modules
from datetime import datetime, timedelta

# get todays date
presentdate = datetime.now()
# work out yesterdays dat
yesterday = presentdate - timedelta(1)
#get tomorrows date
tomorrow = presentdate + timedelta(1)

# print the dates
print("Yesterday was :")
print(yesterday.strftime('%d-%m-%Y'))
print("Today is :")
print(presentdate.strftime('%d-%m-%Y'))
print("Tomorrow would be :")
print(tomorrow.strftime('%d-%m-%Y'))

Lets try this example out

>>> %Run todaytommorowyesterday.py
Yesterday was :
08-04-2022
Today is :
09-04-2022
Tomorrow would be :
10-04-2022

Link

This is in our github repository

todaytommorowyesterday.py

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More