简体   繁体   English

如何在 Python 中将经过时间从秒格式化为小时、分钟、秒和毫秒?

[英]How to format elapsed time from seconds to hours, minutes, seconds and milliseconds in Python?

How can I format the time elapsed from seconds to hours, mins, seconds?如何将经过的时间从秒格式化为小时、分钟、秒?

My code:我的代码:

start = time.time()
... do something
elapsed = (time.time() - start)

Actual Output:实际输出:

0.232999801636

Desired/Expected output:期望/预期输出:

00:00:00.23 

You could exploit timedelta :你可以利用timedelta

>>> from datetime import timedelta
>>> str(timedelta(seconds=elapsed))
'0:00:00.233000'

If you want to include times like 0.232999801636 as in your input:如果您想在输入中包含0.232999801636时间:

import time
start = time.time()
end = time.time()
hours, rem = divmod(end-start, 3600)
minutes, seconds = divmod(rem, 60)
print("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))

Example:例子:

In [12]: def timer(start,end):
   ....:         hours, rem = divmod(end-start, 3600)
   ....:         minutes, seconds = divmod(rem, 60)
   ....:         print("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
   ....:     

In [13]: timer(12345.242,12356.434)
00:00:11.19
In [14]: timer(12300.242,12600.5452)
00:05:00.30
In [19]: timer(0.343,86500.8743)
24:01:40.53
In [16]: timer(0.343,865000.8743)
 240:16:40.53    
In [17]: timer(0,0.232999801636)
00:00:00.23
import time
start = time.time()
#do something
end = time.time()
temp = end-start
print(temp)
hours = temp//3600
temp = temp - 3600*hours
minutes = temp//60
seconds = temp - 60*minutes
print('%d:%d:%d' %(hours,minutes,seconds))

Thestrftime function of time itself can be (ab)used with limitations ( no millisec and <24 hr) time本身的strftime函数可以(ab)使用有限制( 无毫秒和 <24 小时)

elapsed = 4*3600 + 13*60 + 6                       # 15186 s
time.strftime("%Hh%Mm%Ss", time.gmtime(elapsed))   # '04h13m06s'

If you want one line of code without import any other library, that worked for me (Python v3.7.11):如果你想要一行代码而不导入任何其他库,这对我有用(Python v3.7.11):

print("Elapsed time: " + time.strftime("%H:%M:%S.{}".format(str(elapsed % 1)[2:])[:15], time.gmtime(elapsed)))

Output:输出:

Elapsed time: 00:38:01.357318

You can control the milliseconds to be displayed by modifying "[:15]" to "[:11]", then you will get the desired result:您可以通过将“[:15]”修改为“[:11]”来控制要显示的毫秒数,然后您将得到所需的结果:

Elapsed time: 00:45:18.65

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 以天、小时、分钟、秒表示的经过时间 - Python Elapsed Time as Days, Hours, Minutes, Seconds Python:将经过的时间(分钟:秒)解析为秒 - Python: Parse elapsed time (minutes : seconds) to seconds Python时间格式为=&gt; [天:小时:分钟:秒]至秒 - Python time in format => [days:hours:minutes:seconds] to seconds 有没有办法将 timedelta object 格式化为 hours-minutes-seconds.MILLISECONDS? - Is there a way to format a timedelta object to be hours-minutes-seconds.MILLISECONDS? Python从时间间隔中获取小时,分钟和秒 - Python get hours, minutes, and seconds from time interval 从一天中删除小时、分钟和秒,Python - remove the hours, minutes and seconds from the day time, Python Python:有没有办法用写为“小时”:“分钟”:“秒”:“毫秒”的值减去日期时间对象 - Python: Is there a way to subtract date time objects with the values written as “hours”:“minutes”:“seconds”:“milliseconds” Python-将时间转换为小时和分钟,而不是秒 - Python - convert time to hours and minutes, not seconds 如何使用Python格式化分钟/秒? - How to format minutes/seconds with Python? 如何使用我们在 function 中给出的小时、分钟和秒来获取午夜后的毫秒数? (在 Python 中) - How to get milliseconds after midnight with the hours, minutes and seconds we give in the function? (in Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM