Given a current date, the task is to print yesterday’s, today’s, and tomorrow’s date. For example:
If today = 14-11-2025
Yesterday = 13-11-2025
Tomorrow = 15-11-2025
Let’s explore different methods to compute and print these dates in Python.
Using datetime + timedelta
This method uses datetime.now() to get today’s date, then adds or subtracts a timedelta of 1 day to derive yesterday and tomorrow. It automatically handles edge cases like month-end and leap years.
from datetime import datetime, timedelta
presentday = datetime.now()
yesterday = presentday - timedelta(days=1)
tomorrow = presentday + timedelta(days=1)
print("Yesterday:", yesterday.strftime('%d-%m-%Y'))
print("Today:", presentday.strftime('%d-%m-%Y'))
print("Tomorrow:", tomorrow.strftime('%d-%m-%Y'))
Output
Yesterday: 13-11-2025 Today: 14-11-2025 Tomorrow: 15-11-2025
Explanation:
- presentday = datetime.now() retrieves the current local date and time.
- timedelta(days=1) creates a time difference of one day.
- presentday - timedelta(1) gives yesterday and presentday + timedelta(1) gives tomorrow.
- strftime('%d-%m-%Y') formats the date into a readable string.
Using date.today() + timedelta
This version uses the date class instead of datetime. It works directly with only the date part, making it simpler when time is not needed.
from datetime import date, timedelta
today = date.today()
yesterday = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
print("Yesterday:", yesterday.strftime('%d-%m-%Y'))
print("Today:", today.strftime('%d-%m-%Y'))
print("Tomorrow:", tomorrow.strftime('%d-%m-%Y'))
Output
Yesterday: 13-11-2025 Today: 14-11-2025 Tomorrow: 15-11-2025
Explanation:
- date.today() gives only the current date (no time).
- timedelta(days=1) adds/subtracts a day cleanly.
- .strftime() converts the date into dd-mm-yyyy format.
Using calendar Module
This method calculates yesterday/tomorrow using timedelta but uses calendar.day_name to display the day of the week like Monday, Tuesday, etc.
import calendar
from datetime import date, timedelta
today = date.today()
yesterday = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
print("Yesterday:", calendar.day_name[yesterday.weekday()], yesterday.strftime('%d-%m-%Y'))
print("Today:", calendar.day_name[today.weekday()], today.strftime('%d-%m-%Y'))
print("Tomorrow:", calendar.day_name[tomorrow.weekday()], tomorrow.strftime('%d-%m-%Y'))
Output
Yesterday: Thursday 13-11-2025 Today: Friday 14-11-2025 Tomorrow: Saturday 15-11-2025
Explanation:
- calendar.day_name[...] returns day name like "Thursday".
- .weekday() returns day index (0=Monday, 6=Sunday).
Using Timestamp Replacement
This method converts today into a timestamp, manually adjusts the timestamp by adding or subtracting 86400 seconds, then converts it back into a date. It works but is less intuitive. (86400 seconds = 24 hours)
from datetime import datetime, timedelta
today = datetime.now()
yesterday = datetime.fromtimestamp(today.timestamp() - 86400)
tomorrow = datetime.fromtimestamp(today.timestamp() + 86400)
print("Yesterday:", yesterday.strftime('%d-%m-%Y'))
print("Today:", today.strftime('%d-%m-%Y'))
print("Tomorrow:", tomorrow.strftime('%d-%m-%Y'))
Output
Yesterday: 13-11-2025 Today: 14-11-2025 Tomorrow: 15-11-2025
Explanation:
- today.timestamp() converts today's date to seconds.
- - 86400 subtracts 24 hours to get yesterday.
- + 86400 adds 24 hours to get tomorrow.
- datetime.fromtimestamp() converts the timestamp back to a date.