Louna Jushpe
Louna Jushpe

Reputation: 13

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'

Upvotes: 1

Views: 37

Answers (1)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

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

Upvotes: 3

Related Questions