user8128927
user8128927

Reputation:

Error when converting date to specific format

I have the following format for my dates 1952-02-04 00:00:00

I need the format to be month/day/year

How do I go about this currently I have

if clientDob is not None:
            clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d').strftime('%m/%d/%y')

the error I get is

time data 'Birth Dt' does not match format '%Y-%m-%d'

Upvotes: 0

Views: 24

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51683

Use

clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%y')

for datetime parsing your string must match the source string exactly. It contains time-information, you need to match it.

import datetime 
clientDob = "2018-12-21 12:13:14"

clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%y')
print(clientDob)

Output:

12/21/18

Details: strftime-and-strptime-behavior

Upvotes: 1

Related Questions