在Python中格式化时间? [英] Formatting time in Python?
本文介绍了在Python中格式化时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以说我有
time = 9.122491
时间以小时为单位,我需要将其转换为h:m格式。所以所需的输出应该是:
pre $ 9 $ 7
code $ <$ $ p $
任何帮助都将不胜感激! 使用 datetime
模块:
>>> from datetime import datetime,timedelta
>>> mytime = 9.122491
>>> temp = timedelta(hours = mytime)
>>> print((datetime(1,1)+ temp).strftime('%H:%M'))
09:07
How would I go about formatting time in Python?
Lets say I have
time = 9.122491
The time is in hours and I need to convert it to h:m format. So the desired output should be:
9:07
Any help would be greatly appreciated!
解决方案
Using the datetime
module:
>>> from datetime import datetime, timedelta
>>> mytime = 9.122491
>>> temp = timedelta(hours=mytime)
>>> print((datetime(1, 1, 1) + temp).strftime('%H:%M'))
09:07
这篇关于在Python中格式化时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文