Attributeerror: 'str' Object Has No Attribute 'strftime'
I am using the following code to use the date in a specific format and running into following error..how to put date in m/d/y format? from datetime import datetime, date def main
Solution 1:
You should use datetime
object, not str
.
>>>from datetime import datetime>>>cr_date = datetime(2013, 10, 31, 18, 23, 29, 227)>>>cr_date.strftime('%m/%d/%Y')
'10/31/2013'
To get the datetime object from the string, use datetime.datetime.strptime
:
>>>datetime.strptime(cr_date, '%Y-%m-%d %H:%M:%S.%f')
datetime.datetime(2013, 10, 31, 18, 23, 29, 227)
>>>datetime.strptime(cr_date, '%Y-%m-%d %H:%M:%S.%f').strftime('%m/%d/%Y')
'10/31/2013'
Post a Comment for "Attributeerror: 'str' Object Has No Attribute 'strftime'"