Converting minutes to HH:MM format in Python [dupl

2019-02-15 06:08发布

This question already has an answer here:

First of all, I'd like to point out that I'm a beginner with Python.

My problem is that I can't figure out what is the proper way to convert minutes to HH:MM format in Python.

Any help is appreciated!

标签: python time
1条回答
孤傲高冷的网名
2楼-- · 2019-02-15 06:28

Use the divmod() function:

'{:02d}:{:02d}'.format(*divmod(minutes, 60))

Here divmod() divides the minutes by 60, returning the number of hours and the remainder, in one.

Demo:

>>> minutes = 135
>>> '{:02d}:{:02d}'.format(*divmod(minutes, 60))
'02:15'
查看更多
登录 后发表回答