Files
30-seconds-of-code/snippets/days_ago.md
2022-12-04 22:24:48 +02:00

480 B

title, tags, author, cover, firstSeen, lastUpdated
title tags author cover firstSeen lastUpdated
Days ago date maciv blog_images/cup-of-orange.jpg 2020-10-28T16:19:30+02:00 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.
from datetime import timedelta, date

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