Search code examples
pythonstrptime

String to Time Python strptime


I'm pulling the date value from gmail and trying to perform some functions on it. First I simply want to display it, but I can't even do that. See my code and error below.

from datetime import datetime
timeString = 'Sat, 2 Aug 2014 09:29:31 -0700'
myTime = datetime.strptime(timeString, '%m-%d-%Y %I:%M %p')

Here's the error I get. Do you think its the -0700 that's getting in the way?

ValueError: time data 'Sat, 2 Aug 2014 09:29:31 -0700' does not match format '%m-%d-%Y %I:%M %p'

Solution

  • As the error message suggests, you need to put the same format as your date string is in, I've not tried it, but something like this should work:

    myTime = datetime.strptime(timeString, '%a, %d %b %Y %I:%M:%S %z')
    

    Check here for complete details.