Given two dates, Write a Python program to find the total number of days between them.
Input : dt1 = 13/12/2018, dt2 = 25/2/2019 Output : 74 days Input : dt1 = 01/01/2004, dt2 = 01/01/2005 Output : 365 days
Naive Approach: One Naive Solution is to start from dt1 and keep counting days till dt2 is reached. This solution requires more than O(1) time.
A Better and Simple solution is to count total number of days before dt1 from i.e., total days from 00/00/0000 to dt1, then count total number of days before dt2. Finally return the difference between two counts.
Python comes with an inbuilt datetime module which helps us to solve various datetime related problems. In order to find the difference between two dates we simply input the two dates with date type and subtract them, which in turn provides us the number of days between the two dates.
- Find number of days between two given dates
- PHP | Number of week days between two dates
- Program to convert given number of days in terms of Years, Weeks and Days
- Python | Difference between two dates (in minutes) using datetime.timedelta() method
- Date after adding given number of days to the given date
- Python | Sort list of dates given as strings
- C++ program for Sorting Dates using Selection Sort
- How to print date starting from the given date for n number of days using Pandas?
- Comparing dates in Python
- Formatting Dates in Python
- Matplotlib.dates.DateFormatter class in Python
- Matplotlib.dates.ConciseDateFormatter class in Python
- Matplotlib.dates.datestr2num() in Python
- Matplotlib.dates.epoch2num() in Python
- Matplotlib.dates.drange() in Python
- Matplotlib.dates.AutoDateFormatter class in Python
- Python | Pandas TimedeltaIndex.days
- Python | Pandas Timedelta.days
- How to Learn Python in 21 Days?
- How to sort an array of dates in C/C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.