2015-05-29 64 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

相關問題