[Tutor] How to convert seconds to hh:mm:ss format

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Feb 16 23:05:49 CET 2012


>userName = raw_input("Enter your name ")
>     print "hi " + userName + ". \n "
>     seconds = input("Enter the number of seconds since midnight:")
>     hours = seconds/3600
>     hoursRemain = hours%60
>     minutes = hoursReamain/60
>     secondsRemain = minutes%60
>     print "hours: " + "minutes: " + "seconds"

I avoid any kind of manual date/time calculation unless I 
am forced to. Here is how I would do it:

>>> midnight = datetime.datetime.combine( datetime.date.today(), datetime.time() )
>>> seconds = datetime.timedelta( seconds=234 )
>>> time = midnight + seconds
>>> time.strftime( '%H:%M:%S' )
'00:03:54'
>>> time.strftime( 'Hours:%H minutes:%M seconds:%S' )
'Hours:00 minutes:03 seconds:54'

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list