1

I have the following string in Python - 'Oct 01 2013 07 30 PM EST'. I'm trying to convert to a datetime object but am getting the following error:

ValueError: time data 'Oct 01 2013 07 30 PM EST' does not match format
'%b %d %Y %I %M %p %Z'

Not sure where I am going wrong! Please help. My code is below:

from datetime import datetime
date_object = datetime.strptime(str(date), '%b %d %Y %I %M %p %Z')
2
  • 1
    the reason is timezone. please check this question stackoverflow.com/questions/1703546/… Commented Sep 22, 2013 at 19:50
  • Ah, thank you! I just removed the timezone and it works now. Commented Sep 22, 2013 at 23:56

2 Answers 2

3

Python strptime is platform-dependent:

http://docs.python.org/2/library/time.html#time.strptime

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

On my kit (OSX 10.8.5, python 2.7.2), for example, datetime.now().strftime("%Z") is an empty string.

There are external libraries like http://pytz.sourceforge.net/ to help with timezones

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

Comments

1

There are two EST, which cause the confusion: http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations

Eastern Standard Time (North America)   UTC-05
Eastern Standard Time (Australia)       UTC+10

1 Comment

While technically true, that is not the origin of the issue at hand. Try a 3-letter code that is unique (like EDT, eastern daylight time)

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.