blue-sky
blue-sky

Reputation: 53806

Converting string to date that contains 00:00:00

To convert a string date to date format dropping the '00:00:00' I use :

import datetime
strDate = '2017-04-17 00:00:00'
datetime.datetime.strptime(strDate, '%Y/%m/%d %H:%M:%S').strftime('%Y-%m-%d')

Returns :

ValueError: time data '2017-04-17 00:00:00' does not match format '%Y/%m/%d %H:%M:%S'

Is %H:%M:%S not correct format ?

Upvotes: 0

Views: 50

Answers (1)

nyvokub
nyvokub

Reputation: 569

This is the correct way:

datetime.datetime.strptime(strDate, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')

Notice the - instead of / in strptime. The date is converted to: 2017-04-17.

If you would like to have it displayed a different way, have a look here.

Upvotes: 2

Related Questions