0

This is my code:

import datetime
t1 = "Fri 11 Feb 2078 00:05:21"
t2 = "Mon 29 Dec 2064 03:33:48"
t3 = "Sat 02 May 2015 19:54:36"
t3_time = datetime.datetime.strptime(t3,"%a %d %B %Y %H:%M:%S")

print("debug")

t2_time = datetime.datetime.strptime(t2,"%a %d %B %Y %H:%M:%S")
t1_time = datetime.datetime.strptime(t1,"%a %d %B %Y %H:%M:%S")

error: ValueError: time data 'Mon 29 Dec 2064 03:33:48' does not match format '%a %d %B %Y %H:%M:%S'

Why t3 is getting parsed properly whereas t1 and t2 not getting parsed properly?

1 Answer 1

1

You need to give the full name of the month in your input

this should work

import datetime

t1 = "Fri 11 February 2078 00:05:21"
t2 = "Mon 29 December 2064 03:33:48"
t3 = "Sat 02 May 2015 19:54:36"   

print("debug")

t1_time = datetime.datetime.strptime(t1,"%a %d %B %Y %H:%M:%S")
t2_time = datetime.datetime.strptime(t2,"%a %d %B %Y %H:%M:%S")
t3_time = datetime.datetime.strptime(t3,"%a %d %B %Y %H:%M:%S")

Or just use %b instead of %B

import datetime

t1 = "Fri 11 Feb 2078 00:05:21"
t2 = "Mon 29 Dec 2064 03:33:48"
t3 = "Sat 02 May 2015 19:54:36"   

print("debug")

t1_time = datetime.datetime.strptime(t1,"%a %d %b %Y %H:%M:%S")
t2_time = datetime.datetime.strptime(t2,"%a %d %b %Y %H:%M:%S")
t3_time = datetime.datetime.strptime(t3,"%a %d %b %Y %H:%M:%S")

note that adding a textual day in your input won't change anything

for example

t4 = "Fri 02 May 2015 19:54:36"
t4_time = datetime.datetime.strptime(t4,"%a %d %B %Y %H:%M:%S")
print(t3_time == t4_time)

should return True

Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively, one could also use %b instead of %B. The important part is not to mix full and abbreviated month name.
It worked perfectly. 'May' has 3 letter. I got confused with Dec and Feb. I was wondering what was wrong. Anyways, it worked. Thanks a lot. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.