Introduction
In Python, you can easily calculate the date of n days from today using the datetime module. In this challenge, you will be asked to write a function that takes an integer n as input and returns the date of n days from today.
Days from now
Write a function days_from_now(n) that takes an integer n as input and returns the date of n days from today.
To solve this problem, you can follow these steps:
- Import the
datetimemodule. - Use the
date.today()method to get the current date. - Use the
timedeltamethod to addndays to the current date. - Return the new date.
from datetime import timedelta, date
def days_from_now(n):
return date.today() + timedelta(n)
days_from_now(5) ## date(2020, 11, 02)
Summary
In this challenge, you learned how to calculate the date of n days from today using the datetime module in Python. You can use this function to perform various date calculations in your Python programs.