Best way to create date object from unformatted string

Viewed 34

I have the following string which represents a date: '20200317' (yyyyMMdd). Is there any way to pass this string directly to a date object in python? Or I'll have to "break" this string into the year, month and day and then pass it to the date object?

1 Answers

You could use datetime.strptime:

from datetime import datetime
datetimeObject = datetime.strptime('20200317', '%Y%m%d')
Related