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.
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.
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:
datetime
module.date.today()
method to get the current date.timedelta
method to add n
days to the current date.from datetime import timedelta, date
def days_from_now(n):
return date.today() + timedelta(n)
days_from_now(5) ## date(2020, 11, 02)
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.