Python date conversion [closed]
Question
Okay, I give up. Python version 2.7.2
>>> from datetime import datetime
>>> datestr = "2014-01-24"
>>> displaydateobj = datetime.date(datetime.strptime(datestr,'%Y-%d-%m'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 4
What am I missing? I have reviewed the strptime format strings a dozen times.
Solution
Should that be:
datetime.date(datetime.strptime(datestr,'%Y-%m-%d'))
?
OTHER TIPS
Your format string is backwards. It should be %Y-%m-%d
. As you have it, it's trying to put the value 24
into %m
and failing because there is no month 24
Try this:
datetime.strptime(datestr,'%Y-%m-%d').date()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow