Skip to main content

How to Get Yesterday's Date in Python with timedelta

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Reports, backups and data loads often work on "yesterday": the last full day. In Python you get it by subtracting a timedelta of one day from a date, and Python takes care of month ends and leap years for you. This guide shows it on Python 3.15.2.

Syntax​

Syntax
from datetime import timedelta

<earlier date> = <date> - timedelta(days=<number>)
PartWhat it means
<date>A date (or datetime) to count back from
timedelta(days=<number>)A length of time. days=1 is one day
-Moves the date back by that length. + moves it forward

Python's datetime documentation defines date2 = date1 - timedelta as the date that "Computes date2 such that date2 + timedelta == date1."

The Example​

The script counts back one day from 1 December 2026, then from 1 March in a normal year and in a leap year, where the day before falls in February.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

yesterday.py
from datetime import date, timedelta

today = date(2026, 12, 1)
yesterday = today - timedelta(days=1)
print(yesterday)

print(date(2026, 3, 1) - timedelta(days=1))
print(date(2028, 3, 1) - timedelta(days=1))

The yesterday.py script in a macOS-style code window: it subtracts timedelta(days=1) from 1 December 2026, from 1 March 2026 and from 1 March 2028, and prints each result.

The dates are fixed so the output stays the same every time you run it. For the real yesterday, start from today: date.today() - timedelta(days=1), or datetime.now(UTC).date() - timedelta(days=1) for yesterday in UTC.

Step 2: Run It​

Terminal
uv run python yesterday.py
Output
2026-11-30
2026-02-28
2028-02-29

The terminal after uv run python yesterday.py: 2026-11-30, 2026-02-28 and 2028-02-29.

One day before 1 December is 30 November. One day before 1 March is 28 February in 2026 but 29 February in 2028, a leap year. You never handle month lengths yourself. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Subtracting a plain number: date(2026, 12, 1) - 1TypeError: unsupported operand type(s) for -: 'datetime.date' and 'int'Subtract a timedelta: - timedelta(days=1)
Changing the day number by hand: date(2026, 3, today.day - 1)On the first of the month the day becomes 0: ValueError: day 0 must be in range 1..31 for month 3 in year 2026Let timedelta handle it

Troubleshooting​

SymptomLikely causeFix
TypeError: unsupported operand type(s) for -One side isn't a timedeltaWrap the number: timedelta(days=n)
Yesterday is wrong just after midnightThe date came from local time while you wanted UTC, or the other way roundStart from the same time zone your data uses

FAQ​

How do I go back a week?​

timedelta(weeks=1) or timedelta(days=7). Both are seven days.

Does it work with a datetime too?​

Yes. Subtracting timedelta(days=1) from a datetime keeps the time of day and moves the date back one day.

Conclusion​

date - timedelta(days=1) gave 2026-11-30 for 1 December, and found 28 February and 29 February on its own. Subtract a timedelta, never a bare number.

Additional Resources​