Python Date Difference In Days

Chapter: Python Last Updated: 17-04-2023 13:22:57 UTC

Program:

            /* ............... START ............... */
                
from datetime import datetime

date_format = "%Y-%m-%d" # format of the dates
date1_str = "2022-01-01"
date2_str = "2022-02-01"

date1 = datetime.strptime(date1_str, date_format) # convert string to datetime object
date2 = datetime.strptime(date2_str, date_format)

delta = date2 - date1 # difference between dates
print(delta.days) # prints the difference in days

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

Output

If you run the program I provided with the date strings "2022-01-01" and "2022-02-01", 
the output will be: 31

Notes:

  • To calculate the difference between two dates in Python in days, you can use the datetime module.
  • The program is calculating the difference in days between two dates using the datetime module in Python.
  • First, we define the format of the dates using the string "%Y-%m-%d". This specifies that the date string will be in the format of year-month-day.
  • Next, we create two date strings date1_str and date2_str representing the dates we want to compare.
  • Then, we use the strptime() method to convert the date strings into datetime objects. This method takes two arguments: the date string, and the format of the date string.
  • After that, we compute the difference between the two dates by subtracting date1 from date2. This results in a timedelta object representing the difference between the two dates.
  • Finally, we print the number of days between the two dates by accessing the days attribute of the timedelta object. This gives us the number of days between the two dates as an integer.
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