Pythontology Pythontology - 1 year ago 232
Python Question

Parsing non-zero padded timestamps in Python

I want to get datetimes from timestamps like the following :

3/1/2014 9:55
, or something equivalent.

The month, day of month, and hour is not zero padded, but there doesn't seem to be a formatting directive listed here that is able to parse this automatically.

What's the best approach to do so? Thanks!

Answer Source

strptime is able to parse non-padded values. The fact that they are noted as being padded in the formatting codes table applies to strftime's output. So you can just use

datetime.strptime(datestr, "%m/%d/%Y %H:%M")