i am trying to convert a date string to date format
>>> str = "04-18-2002 03:50PM"
>>> time.strptime(str, '%m-%d-%Y %H:%M%p')
time.struct_time(tm_year=2002, tm_mon=4, tm_mday=18, tm_hour=3, tm_min=50, tm_sec=0, tm_wday=3, tm_yday=108, tm_isdst=-1)
however when the year is in two digit it breaks
>>> str = "04-18-02 03:50PM"
>>> time.strptime(str, '%m-%d-%Y %H:%M%p')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '04-18-02 03:50' does not match format '%m-%d-%Y %H:%M'
any ideas??
%Yin the format string denotes a four-digit year, see the documentation. For a two-digit year, use%yinstead. To support both formats, firsttry:one of the formats, and catch theValueErrorand try the other one.