How do I convert seconds to hours, minutes and seconds?

前端 未结 12 2110
执笔经年
执笔经年 2020-11-22 11:00

I have a function that returns information in seconds, but I need to store that information in hours:minutes:seconds.

Is there an easy way to convert the seconds to

相关标签:
12条回答
  • 2020-11-22 11:36
    
    division = 3623 // 3600 #to hours
    division2 = 600 // 60 #to minutes
    print (division) #write hours
    print (division2) #write minutes
    

    PS My code is unprofessional

    0 讨论(0)
  • 2020-11-22 11:37

    Using datetime:

    With the ':0>8' format:

    from datetime import timedelta
    
    "{:0>8}".format(str(timedelta(seconds=66)))
    # Result: '00:01:06'
    
    "{:0>8}".format(str(timedelta(seconds=666777)))
    # Result: '7 days, 17:12:57'
    
    "{:0>8}".format(str(timedelta(seconds=60*60*49+109)))
    # Result: '2 days, 1:01:49'
    

    Without the ':0>8' format:

    "{}".format(str(timedelta(seconds=66)))
    # Result: '00:01:06'
    
    "{}".format(str(timedelta(seconds=666777)))
    # Result: '7 days, 17:12:57'
    
    "{}".format(str(timedelta(seconds=60*60*49+109)))
    # Result: '2 days, 1:01:49'
    

    Using time:

    from time import gmtime
    from time import strftime
    
    # NOTE: The following resets if it goes over 23:59:59!
    
    strftime("%H:%M:%S", gmtime(125))
    # Result: '00:02:05'
    
    strftime("%H:%M:%S", gmtime(60*60*24-1))
    # Result: '23:59:59'
    
    strftime("%H:%M:%S", gmtime(60*60*24))
    # Result: '00:00:00'
    
    strftime("%H:%M:%S", gmtime(666777))
    # Result: '17:12:57'
    # Wrong
    
    0 讨论(0)
  • 2020-11-22 11:39

    By using the divmod() function, which does only a single division to produce both the quotient and the remainder, you can have the result very quickly with only two mathematical operations:

    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    

    And then use string formatting to convert the result into your desired output:

    print('{:d}:{:02d}:{:02d}'.format(h, m, s)) # Python 3
    print(f'{h:d}:{m:02d}:{s:02d}') # Python 3.6+
    
    0 讨论(0)
  • 2020-11-22 11:42

    You can divide seconds by 60 to get the minutes

    import time
    seconds = time.time()
    minutes = seconds / 60
    print(minutes)
    

    When you divide it by 60 again, you will get the hours

    0 讨论(0)
  • 2020-11-22 11:45

    You can use datetime.timedelta function:

    >>> import datetime
    >>> str(datetime.timedelta(seconds=666))
    '0:11:06'
    
    0 讨论(0)
  • 2020-11-22 11:46

    I can hardly name that an easy way (at least I can't remember the syntax), but it is possible to use time.strftime, which gives more control over formatting:

    from time import strftime
    from time import gmtime
    
    strftime("%H:%M:%S", gmtime(666))
    '00:11:06'
    
    strftime("%H:%M:%S", gmtime(60*60*24))
    '00:00:00'
    

    gmtime is used to convert seconds to special tuple format that strftime() requires.

    Note: Truncates after 23:59:59

    0 讨论(0)
提交回复
热议问题