Python Program to Find Number of Days Between Two Given Dates

In this tutorial, we will discuss how to write a Python program to find the number of days between two given numbers.

Suppose we have given two dates our expected output would be:

Example:

Method 1: Naïve Approach

In this approach, the naïve solution will start from date_1, and it will keep counting the number of days until it reaches date_2. This solution will require more than O(1) times. It is a simple solution for counting the total number of days before date_1, which means it will count total days from 00/00/0000 to date_1, then it will count the total number of days before date_2. At last, it will return the difference between the two counts in the form of total days between the two given dates.

Example:

Output:

Number of Days between the given Dates are:  322 days

Method 2: By using Python datetime module

In this method, we will see how we can use a built_in function of Python "datetime", which can help the users in solving various date-time related problems. For finding the difference between two dates, we can input the two dates in a date type format and subtract them, and this will result in output as the number of days between the two given dates.

Example:

Output:

Number of Days between the given Dates are:  513 days

Conclusion

In this tutorial, we have discussed two different methods of how to write a python code to find the total number of days between the two given dates.






Latest Courses