Convert string of dd-MON-yy to date in Python

Viewed 32

I have a string in dd-MON-yy format. While converting to date in python, its is causing issue since the year is in tow digits.

datetime.datetime.strptime('17-JUN-03', '%d-%m-%y')

The error is,

ValueError: time data '17-JUN-03' does not match format '%d-%m-%y'
1 Answers

Try this:

import datetime
print(datetime.datetime.strptime('17-JUN-03', '%d-%b-%y'))

Result:

2003-06-17 00:00:00

Datetime format codes

Related