简体   繁体   中英

format error in python string to time conversion

When I try to display the string in time, I get a format error.

result3 = ''.join(file_name)


date_time_str = result3

result1 = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')





print(result1)

The problem here is that ValueError:time data 20220308233355'does not match format '%Y-%m-%d%H:%M:%S' error occurs. Did I enter the wrong format? I want to mark 20220308233355 as yyyy-mm--dd hh:mm:ss.

yes, you wanna do this:

result1 = datetime.datetime.strptime(date_time_str, '%Y%m%d%H%M%S')
print(result1)
>> 2022-03-08 23:33:55

which is datetime type, if you want string in that format, you have to:

result1 = result1.strftime('%Y-%m-%d %H:%M:%S'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM