How To Fix Valueerror Time Date Does Not Match Format When Converting From String To Datetime
Solution 1:
%Z is generally used for converting into string format. In any case, it is the offset, not the name of the time zone.
The rest of your code is valid, however:
s = "Mon, 22 Apr 2019 17:04:38 +0200"d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z')
Solution 2:
datetime
only comes with the ability to parse UTC
and whatever local time zone is listed in time.tzname
. It can't match (CEST)
because it doesn't know what timezone that is (It would also be redundant because you defined the timezone using the offset +0200
).
You will need to implement your own (CEST)
using datetime.tzinfo
or by importing an external library like pytz
or pendulum
in order to parse (CEST)
from a string into a datetime.timezone
.
Also, don't forget to include parenthesis()
in your match string.
Solution 3:
This code passes, however, I do not know what happens to 'CEST' once it is converted into the string.
from datetime importdatetimetz='CEST'
s = "Mon, 22 Apr 2019 17:04:38 +0200 " + tzd= datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z ' + tz)
Post a Comment for "How To Fix Valueerror Time Date Does Not Match Format When Converting From String To Datetime"