0

I am getting this String:

a = "Wed Mar 06 2019 17:35:33 GMT-0500 (Ecuador Time)"

and i am trying to parse it in python with this code:

fecha = datetime.strptime(a , '%a %b %d %Y %H:%M:%S GMT%z (%Z)')

But i am getting an error

ValueError: time data 'Wed Mar 06 2019 17:35:33 GMT-0500 (Ecuador Time)' does not match format '%a %b %d %Y %H:%M:%S GMT%z (%Z)'

what am i doing wrong? i've try a lot of settings for the date but still can get a solution my goal is to get a date object and save it into a MongoDatabase

1 Answer 1

5

The problem with the code is "(Ecuador Time)" is not in the format required to match "(%Z)".

To fix your code, you can just simply remove "(Ecuador Time)" from your string and remove your "(%Z)". Since you already have your GMT offset, it shouldn't matter too much.

This would leave your code as:

from datetime import datetime

a = "Wed Mar 06 2019 17:35:33 GMT-0500"
fetcha = datetime.strptime(a , '%a %b %d %Y %H:%M:%S GMT%z')
2
  • Thank you so much this works i split the string this way: texto = a.split() texto = texto[0]+" "+texto[1]+" "+texto[2]+" "+texto[3]+" "+texto[4]+" "+texto[5] and then i could parse it thank you again :) Mar 7, 2019 at 13:45
  • I would recommend doing something like a = "Wed Mar 06 2019 17:35:33 GMT-0500 (Ecuador Time)" and then a = a.split(' (')[0] instead, but your way should work as well.
    – Nick Vitha
    Mar 7, 2019 at 17:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.