I want to convert a string to a date format using Python datetime.strptime.
However, I set the format to '%m/%d/%y' but the result is '%Y-%m-%d%H:%M:%S'. What's the reason?
convert.py
date_str = ['11/10/2021', '12/15/2021', '2/26/2022', '3/10/2021', '3/10/2021', '3/11/2021']
replace_format = datetime.strptime(date_str, '%m/%d/%Y')
result
print(replace_format)
2022-03-14 00:00:00
2022-04-13 00:00:00
2022-05-11 00:00:00
2022-06-16 00:00:00
2022-06-18 00:00:00
2022-06-18 00:00:00
datetime, when you call print on it is used, which is'%Y-%m-%d %H:%M:%S'. You can usereplace_format.strftime('%m/%d/%y')to achieve the desired format.strptimeparses a string to a datetime object. The datetime object's default representation (as a string) happens to be%Y-%m-%d %H:%M:%S. But that's just a matter how it is displayed to you when you print it.strftimedoes not change anything in the object, it is just a string representation of it.