在 Python 中将分钟转换为 HH:MM 格式 [英] Converting minutes to HH:MM format in Python
本文介绍了在 Python 中将分钟转换为 HH:MM 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我想指出我是 Python 的初学者.
我的问题是我不知道在 Python 中将分钟转换为 HH:MM 格式的正确方法是什么.
感谢任何帮助!
解决方案
使用 divmod()
函数:
'{:02d}:{:02d}'.format(*divmod(minutes, 60))
此处 divmod()
将分钟除以 60,返回小时数和余数合一.
演示:
<预><代码>>>>分钟 = 135>>>'{:02d}:{:02d}'.format(*divmod(minutes, 60))'02:15'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!
解决方案
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'
这篇关于在 Python 中将分钟转换为 HH:MM 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文