Python: convert seconds to hh:mm:ss [duplicate]

2019-01-16 23:15发布

This question already has an answer here:

Please, how to convert an int (number a seconds) to these formats: mm:ss or hh:mm:ss ?

I need to do this with Python code (and if possible in a Django template ?).

Thank you very much ;-)

10条回答
仙女界的扛把子
2楼-- · 2019-01-16 23:27

You can calculate the number of minutes and hours from the number of seconds by simple division:

seconds = 12345
minutes = seconds // 60
hours = minutes // 60

print "%02d:%02d:%02d" % (hours, minutes % 60, seconds % 60)
print "%02d:%02d" % (minutes, seconds % 60)

Here // is pythons integer division.

查看更多
再贱就再见
3楼-- · 2019-01-16 23:27

Not being a python person but easiest without any libraries just:

total   = 3800
seconds = total % 60
total   = total - seconds
hours   = total / 3600
total   = total - (hours * 3600)
mins    = total / 60

Updated code, thanks sth

查看更多
【Aperson】
4楼-- · 2019-01-16 23:31

Besides the fact that Python has built in support for dates and times (see bigmattyh's response), finding minutes or hours from seconds is easy:

minutes = seconds / 60
hours = minutes / 60

Now, when you want to display minutes or seconds, MOD them by 60 so that they will not be larger than 59

查看更多
时光不老,我们不散
5楼-- · 2019-01-16 23:32

Code that does what was requested, with examples, and showing how cases he didn't specify are handled:

def format_seconds_to_hhmmss(seconds):
    hours = seconds // (60*60)
    seconds %= (60*60)
    minutes = seconds // 60
    seconds %= 60
    return "%02i:%02i:%02i" % (hours, minutes, seconds)

def format_seconds_to_mmss(seconds):
    minutes = seconds // 60
    seconds %= 60
    return "%02i:%02i" % (minutes, seconds)

minutes = 60
hours = 60*60
assert format_seconds_to_mmss(7*minutes + 30) == "07:30"
assert format_seconds_to_mmss(15*minutes + 30) == "15:30"
assert format_seconds_to_mmss(1000*minutes + 30) == "1000:30"

assert format_seconds_to_hhmmss(2*hours + 15*minutes + 30) == "02:15:30"
assert format_seconds_to_hhmmss(11*hours + 15*minutes + 30) == "11:15:30"
assert format_seconds_to_hhmmss(99*hours + 15*minutes + 30) == "99:15:30"
assert format_seconds_to_hhmmss(500*hours + 15*minutes + 30) == "500:15:30"

You can--and probably should--store this as a timedelta rather than an int, but that's a separate issue and timedelta doesn't actually make this particular task any easier.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-16 23:33

If you need to do this a lot, you can precalculate all possible strings for number of seconds in a day:

try:
    from itertools import product
except ImportError:
    def product(*seqs):
        if len(seqs) == 2:
            for s1 in seqs[0]:
                for s2 in seqs[1]:
                    yield (s1,s2)
        else:
            for s in seqs[0]:
                for p in product(*seqs[1:]):
                    yield (s,) + p

hhmmss = {}
i = 0
for (h,m,s) in product(range(24),range(60),range(60)):
    hhmmss[i] = "%02d:%02d:%02d" % (h,m,s)
    i += 1

Now conversion of seconds to format string is a fast dict lookup:

print hhmmss[12345]

prints

'03:25:45'
查看更多
Fickle 薄情
7楼-- · 2019-01-16 23:45

Have you read up on the datetime module?

Edit/update: SilentGhost's answer has the details my answer leaves out. If you like this answer, +1 his as well (or instead). Reposted here:

>>> a = datetime.timedelta(seconds=65)
datetime.timedelta(0, 65)
>>> str(a)
'0:01:05'
查看更多
登录 后发表回答