Files
30-seconds-of-code/snippets/days_ago.md
Isabelle Viktoria Maciohsek 59c028870b Add days_ago
2020-10-28 16:19:30 +02:00

366 B

title, tags
title tags
days_ago 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.
from datetime import timedelta, date

def days_ago(n):
  return date.today() - timedelta(n)
days_ago(5) # date(2020, 10, 23)