[Tutor] 12 hr to 24 hr time conversion

Peter Otten __peter__ at web.de
Wed Oct 26 11:23:10 CEST 2011


Praveen Singh wrote:

> How can i convert this-
> 
>>>>time24hr('12:34am')
>  '0034hr'
> 
> i searched in date module but i am not able to figure out what how to do
> this...

>>> time.strftime("%H:%M", time.strptime("12:34am", "%I:%M%p"))
'00:34'

>>> datetime.datetime.strptime("12:34am", "%I:%M%p").strftime("%H:%M")
'00:34'

>>> datetime.datetime.strptime("12:34am", "%I:%M%p").strftime("%H:%Mhr")
'00:34hr'

The supported formats are explained here:

http://docs.python.org/library/time.html#time.strftime



More information about the Tutor mailing list