Add days_ago

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-28 16:19:30 +02:00
parent 2666e7d653
commit 59c028870b

20
snippets/days_ago.md Normal file
View File

@ -0,0 +1,20 @@
---
title: days_ago
tags: date,intermediate
---
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)
```