flpn
flpn

Reputation: 1978

Best way to create date object from unformatted string

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?

Upvotes: 2

Views: 40

Answers (1)

Mureinik
Mureinik

Reputation: 312219

You could use datetime.strptime:

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

Upvotes: 4

Related Questions