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 Program to Find the Number Occurring Odd Number of Times
- Python Program for Find sum of even factors of a number
- Python Program for Find sum of odd factors of a number
- Python program to find smallest number in a list
- Python program to find largest number in a list
- Python Program for Find minimum sum of factors of number
- Python program to find second largest number in a list
- Python program to find number of local variables in a function
- Python Program for Find largest prime factor of a number
- C++ program for Sorting Dates using Selection Sort
- Comparing dates in Python
- Python | Sort list of dates given as strings
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.