skoeb
skoeb

Reputation: 875

strftime with midnight formatted as '24' rather than '00'

I have a .csv with a series of timestamps with the format HH:MM:SS. Annoyingly, the hour is formatted with midnight represented as 24 rather than 00.

Normally I would use: datetime.strptime('24:00:00', '%H:%M:%S'), but this throws a ValueError as 24 isn't recognized as a valid hour.

Is there an easy way to do this short of manipulating the string?

Upvotes: 0

Views: 865

Answers (1)

Oliver Chen
Oliver Chen

Reputation: 21

I don't think there is an easy way using the datetime library. You can check out the source code for datetime.strptime here; the relevant regex is on line 188. The simplest solution may be string manipulation.

timestamp = '24:00:00'

if timestamp[:2] == '24':
    timestamp = '00' + timestamp[2:]

#as a oneliner
timestamp = '00' + timestamp[2:] if timestamp[:2] == '24' else timestamp

Upvotes: 1

Related Questions