From 7b576c1aa580d3bae257710c461d17a79885dda6 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Wed, 28 Oct 2020 16:19:51 +0200 Subject: [PATCH] Add days_from_now --- snippets/days_from_now.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/days_from_now.md diff --git a/snippets/days_from_now.md b/snippets/days_from_now.md new file mode 100644 index 000000000..79ccfd7a6 --- /dev/null +++ b/snippets/days_from_now.md @@ -0,0 +1,20 @@ +--- +title: days_from_now +tags: date,intermediate +--- + +Calculates the date of `n` days from today. + +- Use `datetime.date.today()` to get the current day. +- Use `datetime.timedelta` to add `n` days from today's date. + +```py +from datetime import timedelta, date + +def days_from_now(n): + return date.today() + timedelta(n) +``` + +```py +days_from_now(5) # date(2020, 11, 02) +```