Pythontology Pythontology - 6 months ago 70
Python Question

Parsing non-zero padded timestamps in Python

I want to get datetimes from timestamps like the following :

3/1/2014 9:55
with
datetime.strptime
, 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

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")