當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python timedelta total_seconds()用法及代碼示例

Python timedelta.total_seconds() 方法

timedelta.timedeltotal_seconds() 方法用於 datetime 模塊的 timedelta 類。

它使用該類的實例並返回該時間實例的給定持續時間中涵蓋的總秒數。

模塊:

    import datetime

類:

    from datetime import timedelta

用法:

    total_seconds()

參數:

  • None

返回值:

此方法的返回類型是一個數字,即該時間段內涵蓋的總秒數。

例:

## Python program to illustrate 
## the use of total_seconds function
from datetime import time, timedelta  
    
## total_seconds function
x = timedelta(minutes = 2*15)
total = x.total_seconds()
print("Total seconds in 30 minutes:", total)
print()

## time can be negative also 
x = timedelta(minutes = -2*15)
total = x.total_seconds()
print("Total seconds:", total)
print()

x = timedelta(days = 1, minutes = 50, seconds = 56)
total = x.total_seconds()
print("Total seconds in the given duration:", total)
print()

x = timedelta(hours=1,minutes= 50,seconds= 40)
y = timedelta(hours=10,minutes= 20,seconds= 39)
d = y-x
print("Total seconds covered in subtracting:", d.total_seconds())
print()

x = timedelta(hours=1,minutes= 50,seconds= 40)
y = timedelta(hours=10,minutes= 20,seconds= 39)
d = y+x
print("Total seconds covered in adding:", d.total_seconds())
print()

x = timedelta(hours=1,minutes= 50,seconds= 40)
y = timedelta(hours=10,minutes= 20,seconds= 39)
d = y%x
print("Total seconds remaining when y is divided by x", d.total_seconds())
print()

輸出

Total seconds in 30 minutes:1800.0

Total seconds:-1800.0

Total seconds in the given duration:89456.0

Total seconds covered in subtracting:30599.0

Total seconds covered in adding:43879.0

Total seconds remaining when y is divided by x 4039.0


相關用法


注:本文由純淨天空篩選整理自 Python timedelta total_seconds() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。