Files
30-seconds-of-code/snippets/days_ago.md
Isabelle Viktoria Maciohsek e62b22659d Update snippet titles
2022-02-13 13:53:22 +02:00

23 lines
442 B
Markdown

---
title: Days ago
tags: date,intermediate
firstSeen: 2020-10-28T16:19:30+02:00
lastUpdated: 2020-10-28T16:19:30+02:00
---
Calculates the date of `n` days ago from today.
- Use `datetime.date.today()` to get the current day.
- Use `datetime.timedelta` to subtract `n` days from today's date.
```py
from datetime import timedelta, date
def days_ago(n):
return date.today() - timedelta(n)
```
```py
days_ago(5) # date(2020, 10, 23)
```