简体   繁体   中英

I'm getting an error message regarding datetime

I'm trying to turn a date with hours into just a date.

Please help me, this is driving me insane.

This is my script:

from datetime import datetime
Date1= '2018-2-12 10:30:01'
d = datetime.strptime('Date1','%Y-%m-%d %H:%M:%S')
day_string = d.strftime('%Y-%m-%d')

and this is the error message:

ValueError: time data 'Date1' does not match format '%Y-%m-%d %H:%M:%S'

You should use the variable Date1 instead of the string 'Date1'

from datetime import datetime
Date1= '2018-2-12 10:30:01'
d = datetime.strptime(Date1,'%Y-%m-%d %H:%M:%S')
day_string = d.strftime('%Y-%m-%d')
print(day_string)
#2018-02-12

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