2015-05-29 63 views
0

剪掉让我的想法实现在Python的Python秒钟小时

以下
total_hrs = datetime.timedelta(hours=total_hrs) 

这将导致在3点52分三十秒。但我需要修剪几秒钟。这一点应在3时52分00秒

+0

格式什么是你'total_hrs'变量的原始内容? – dlask

+0

total_hrs = 3.875这个浮点值实际上是 –

回答

1
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 
3
result = datetime.timedelta(minutes=int(total_hours*60)) 
+0

这比我的方式有点整洁! – tom

相关问题