Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

does not match format datetimein python

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading