以小时为单位表示时间以详细时间

时间:2016-01-29 16:36:34

标签: python datetime

我需要在小时和分钟表达我的时间。这就是我所拥有的:

0.0425 hours

~153秒,如何将其显示为0 Hours 2 minutos 33 seconds

2 个答案:

答案 0 :(得分:3)

这是一种方式。

time = '0.0425 hours'


# Extract time from string
time = time.split()[0]

# Convert time to a number of hours, then a number of seconds
time = float(time)
time = int(time * 3600)

# Compute the constituent elements of the time
hours = time // 3600
minutes = (time // 60) % 60
seconds = time % 60

# Print the result
print '{hours} Hours {minutes} minutos {seconds} seconds'.format(
    hours=hours, minutes=minutes, seconds=seconds)

答案 1 :(得分:1)

import time
import re

secs = int(re.sub('\D', '','~153'))
print '%d hours %d minutos %s seconds'%(time.gmtime(secs)[3:6])