Python秒从小时缩短

2024-05-16 01:15:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python实现以下几点

total_hrs = datetime.timedelta(hours=total_hrs)

结果是3:52:30。但我得缩短时间。这应该是3:52:00的格式


Tags: datetime格式时间timedeltatotalhourshrs
2条回答
total_hrs = datetime.timedelta(hours=3.875)
print str(total_hrs)
>>> 3:52:30

# Split into components
total_hrs_split = str(total_hrs).split(':')
print total_hrs_split
>>> ['3', '52', '30']

# Trim off seconds
total_hrs_trimmed=datetime.timedelta(hours=int(total_hours_split[0]),
                                     minutes=int(total_hours_split[1]))
print str(total_hrs_trimmed)
>>> 3:52:00
result = datetime.timedelta(minutes=int(total_hours*60))

相关问题 更多 >