How to add days to date in Python
In this article, we will learn how to add days to dates in Python. We will use some built-in modules available and some custom codes as well to see them working. Let's first have a quick look over what are dates in Python and then how can we compare them in Python.
Dates in Python
In Python, we can work on Date functions by importing a built-in module
datetime available in Python. We have date objects to work with dates. We have time objects to work with time, date, and days as well. This datetime module contains date in the form of year, month, day, hour, minute, second, and microsecond. The datetime module has many methods to return information about the date object. It requires date, month, and year values to compute the function. Date and time functions are compared like mathematical expressions between various numbers.
In this article, we will learn about three different way in which we can add days to a date in python-
Using datetime.timedelta() Function
Using timedelta in datetime Module
Using Pandas Module
In order to add days to a specific date, we will have to use the below module.
datetimemodule: DateTime objects give date along with time in hours, minutes, seconds. The DateTime library provides manipulation to a combination of both date and time objects (month, day, year, seconds, and microseconds).
timedeltamodule: Timedelta class represents the duration. The DateTime library provides timedelta method to carry out date-related manipulation and also calculate differences in time objects. It can be majorly used to perform arithmetic operations like addition, subtraction, and multiplication. By specifying the days attribute value, we can add days to the date specified.
Example: Add Days to a Date using datetime.timedelta() Function
In Python, we have a built-in datetime module It provides a
datetime.timedelta() method which processes timedelta object. The number of days to be added is passed as an argument to the timedelta function and it returns the date in the given format that is
year-month-date-hours:minutes:seconds. Another function of datetime is also used here that is
datetime.strptime() method of datetime() module.
import datetime current_date = "12/6/20" current_date_temp = datetime.datetime.strptime(curr_date, "%m/%d/%y") newdate = current_date_temp + datetime.timedelta(days=5) print(new_date)
Explanation- This example takes your given current date. A new variable is created to store the result of strptime() function. strptime() function takes the current date and specifiers to store in a variable. The new variable is added to timedelta whose parameter takes 5 as the number of days. The resultant date is showing 5 days extra from the given previous date.
Example: Add Days to the Current Date using timedelta
The Python built-in datetime module has the timedelta method itself besides the timedelta from its submodule datetime. The timedelta() method takes the number of days to be added as its argument and returns them in a date format that is year - month - date. The date module also has a
today() method, which returns the current date.
from datetime import timedelta, date Date_req = date.today() + timedelta(days=5) print(Date_req)
Explanation- This example is quite easy and uses one line of code to add the days to date. The current date is taken from
date.today() function and 5 is added to the current day and the result is printed.
Example: Add Days to a Date using Pandas Module
Pandas module provides a
to_datetime() method that takes a date as its argument and converts it into a
pandas._libs.tslibs.timestamps. Timestamp object. Pandas will perform smart converting if the format of the date string is not given.
DateOffset() - This method takes the keyword arguments like days, months and etc. It returns a pandas.tseries.offsets.DateOffset object.
import pandas as pd initial_date = "2019-05-12" req_date = pd.to_datetime(initial_date) + pd.DateOffset(days=3) print(req_date)
Explanation- This example takes any date in a variable of your choice. It uses to_datetime() and passes the initial date as an argument to convert the initial date into datetime format. Similarly, dateoffset function is used to 3 days to the initial day and we get the resultant date.
In this article, we learned to add days to dates by passing the number of days to an argument either by using
datetime module or
timedelta module. We also used Pandas to add date using
DateOffeset() the function. We used some custom codes as well. For example, we used a date example in the given format and add days to date.