How do I know the difference of days between two dates using Python? For example, to know how many days there are between 11/22/213 and 3/25/2014 considering a possible leap year.
How do I know the difference of days between two dates using Python? For example, to know how many days there are between 11/22/213 and 3/25/2014 considering a possible leap year.
Just instantiate the two dates as datetime.date
objects and subtract them:
In [1]: import datetime
In [2]: data1 = datetime.date(day=22, month=11, year=2013)
In [3]: data2 = datetime.date(day=25, month=3, year=2014)
In [4]: data2-data1
Out[4]: datetime.timedelta(123)
In [5]: diferenca = data2-data1
In [6]: diferenca.days
Out[6]: 123
There's another way too:
# -*- coding: utf-8 -*-
from datetime import datetime
def diff_days(date1, date2):
d1 = datetime.strptime(date1, "%d-%m-%Y")
d2 = datetime.strptime(date2, "%d-%m-%Y")
return abs((date2 - date1).days)