Get the Amount of Days Between Two Dates in Python
Let’s say that you have two dates:
How would you create a function that would return the number of days between these two dates?
Introducing the Datetime Package
Python comes with a built-in library called datetime
.
We start by importing the date
module.
|
|
With this date module, we have access to an object format of the date
type.
Writing our Function
Next, we should write our function that takes in two dates and returns the number of days between them.
|
|
We created lists of our dates to be able to use them in the `date` function:
As the date
function takes in integer values, we can now use the appropriate index locations of our date lists.
Testing our Function
Let’s test our function out: