Philip K. Dick
Philip K. Dick

Reputation: 77

How to convert from '%Y:%m:%d' to date in Python?

I'm using the time module and the strptime() function however it's erroring and I'm pretty sure I have the format right.

ds = '2014:10:9'
d = time.strptime('%Y:%m:%d', ds)

I get this exception:

ValueError: time data '%Y:%m:%d' does not match format '2014:10:9'

From reading the docs on strptime(), I have the right pattern, what am I missing?

Upvotes: 2

Views: 1031

Answers (1)

peluzza
peluzza

Reputation: 329

Wrong. Use:

d = datetime.datetime.strptime(ds, '%Y:%m:%d')

Mind the order of arguments. @Marc B : Strptime is able to parse non-padded values.

Upvotes: 2

Related Questions