In todays post we will be creating a script that will give us the difference in days between two dates, to do this we will be using the ‘datetime’ module which supplies classes for manipulating dates and times.
We start by defining two dates and then pass these to our ‘get_days’ function. Here we will parse our dates in to the relevant datetime format and use the minus operator to get the difference.
Full source code can be found below.
from datetime import datetime
def get_days(d1, d2):
d1 = datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.strptime(d2, "%Y-%m-%d")
return abs((d2 - d1).days)
d1='2022-01-01'
d2='2022-01-31'
print(get_days(d1,d2))