Add Days to a Given Date

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, you can easily add or subtract days from a given date using the datetime module. This can be useful in various scenarios, such as calculating the due date of a task or project, or determining the date after a certain number of days from a given date.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/date_time("`Date and Time`") subgraph Lab Skills python/comments -.-> lab-13584{{"`Add Days to a Given Date`"}} python/tuples -.-> lab-13584{{"`Add Days to a Given Date`"}} python/function_definition -.-> lab-13584{{"`Add Days to a Given Date`"}} python/default_arguments -.-> lab-13584{{"`Add Days to a Given Date`"}} python/importing_modules -.-> lab-13584{{"`Add Days to a Given Date`"}} python/using_packages -.-> lab-13584{{"`Add Days to a Given Date`"}} python/standard_libraries -.-> lab-13584{{"`Add Days to a Given Date`"}} python/date_time -.-> lab-13584{{"`Add Days to a Given Date`"}} end

Add Days to Date

Write a function add_days(n, d) that takes in two arguments:

  • n: an integer representing the number of days to add (if positive) or subtract (if negative) from the given date.
  • d: an optional argument representing the date to which the days should be added or subtracted. If not provided, the current date should be used.

The function should return a datetime object representing the new date after adding or subtracting the specified number of days.

from datetime import datetime, timedelta

def add_days(n, d = datetime.today()):
  return d + timedelta(n)
from datetime import date

add_days(5, date(2020, 10, 25)) ## date(2020, 10, 30)
add_days(-5, date(2020, 10, 25)) ## date(2020, 10, 20)

Summary

In this challenge, you learned how to add or subtract days from a given date using the datetime module in Python. You also wrote a function that takes in a number of days and a date, and returns a new date after adding or subtracting the specified number of days.

Other Python Tutorials you may like