Python Add Days To Date

Chapter: Python Last Updated: 16-04-2023 12:06:10 UTC

Program:

            /* ............... START ............... */
                
from datetime import datetime, timedelta

date_string = "2022-01-01"
date = datetime.strptime(date_string, "%Y-%m-%d")

days_to_add = 7
new_date = date + timedelta(days=days_to_add)

new_date_string = new_date.strftime("%Y-%m-%d")
print(new_date_string)

                /* ............... END ............... */
        

Output

2022-01-08
In this program we have added 7 days to the original date of "2022-01-01". 
The resulting date is "2022-01-08".

Notes:

  • The program is a Python code snippet that demonstrates how to add a specified number of days to a date using the datetime module.
  • First, the code imports the necessary modules: datetime and timedelta. The datetime module provides various classes for working with dates and times, and the timedelta module provides a way to represent a duration or a difference between two dates or times.
  • Next, the program defines a date as a string in the format "YYYY-MM-DD" using the variable date_string. In this case, the date is set to "2022-01-01".
  • Then, the code uses the strptime method from the datetime module to parse the date string and convert it into a datetime object called date. The strptime method takes two arguments: the date string to parse and the format in which the date is provided. In this case, the format is "%Y-%m-%d", which specifies that the date string is in the format "YYYY-MM-DD".
  • The program then defines a variable called days_to_add and sets it to 7. This is the number of days that will be added to the original date.
  • To add the specified number of days to the date, the program creates a new datetime object called new_date by adding a timedelta object to the original date. The timedelta object is created using the timedelta constructor from the datetime module and takes one argument: the number of days to add to the date. In this case, we pass days_to_add to the timedelta constructor to specify that we want to add 7 days to the original date.
  • Finally, the program converts the new_date object back into a string using the strftime method from the datetime module. The strftime method takes one argument: the format in which to represent the date as a string. In this case, the format is again set to "%Y-%m-%d", which represents the date as a string in the format "YYYY-MM-DD". The resulting string is then printed to the console. In this case, the output is "2022-01-08", which is the original date plus 7 days

Tags

How to add days to date in python #Python Add Days To Date

Similar Programs Chapter Last Updated
Python Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023

1 2 3 4