Reputation: 13
I want to find the difference between two dates
def days_between(d1, d2):
d1 = datetime.strptime(d1, "%d-%m-%Y")
d2 = datetime.strptime(d2, "%d-%m-%Y")
return abs((d2 - d1).days)
x = datetime(2022, 1, 12)
y = x.strftime("%x")
xy = datetime.now()
print(days_between(y, xy.strftime("%x")))
But this error is coming
Traceback (most recent call last): File "/home/user/Documents/python/i-bot-website/test.py", line 15, in print(days_between(y, xy.strftime("%x"))) File "/home/user/Documents/python/i-bot-website/test.py", line 6, in days_between d1 = datetime.strptime(d1, "%d-%m-%Y") File "/usr/lib/python3.9/_strptime.py", line 568, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) File "/usr/lib/python3.9/_strptime.py", line 349, in _strptime raise ValueError("time data %r does not match format %r" % ValueError: time data '01/12/22' does not match format '%d-%m-%Y'
Upvotes: 1
Views: 68
Reputation: 76
def days_between(d1, d2):
d1 = datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.strptime(d2, "%Y-%m-%d")
return abs(d2 - d1)
v = datetime(2022,2,2)
s = datetime.now().strftime("%Y-%m-%d")
print(days_between(v.strftime("%Y-%m-%d"),s))
Upvotes: 0
Reputation: 78
Try this format
x = datetime(2022, 1, 12)
y = x.strftime("%d-%m-%Y")
xy = datetime.now()
print(days_between(y, xy.strftime("%d-%m-%Y")))
Upvotes: 0