简体   繁体   中英

converting string into date format with Python

Hi I am trying to convert this string in Python : '2019-01-10' into date format as 2019-01-10

I have tried this code :

from datetime import datetime

date_time_str = '2019-01-10'

date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d )

but i have a ValueError : time data '2019-01-10' does not match format '%y-%m-%d'

Can you help me please ?

%y format specifier is 2 digits, use uppercase %Y . Ref: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

from datetime import datetime
date_time_str = '2018-06-29'
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d')

try changing it to the above code.

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