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


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


total_seconds() 函數用於返回指定時間實例持續時間所涵蓋的總秒數。該函數用於 DateTime 模塊的 timedelta 類。

用法:total_seconds()

參數:此函數不接受任何參數。

Return values:此函數返回指定的時間實例持續時間所涵蓋的總秒數。

範例1:在下麵的示例中,將在 total_seconds() 函數的幫助下根據第二個值返回 55 分鍾。



Python3


# Python3 code for getting
# total seconds for the
# given duration of time
# Importing time and timedelta module
from datetime import time, timedelta 
     
# Specifying a time duration
A = timedelta(minutes = 55)
# Calling the total_seconds() function
# over the specified time duration
totalsecond = A.total_seconds()
# Getting the Total seconds for
# the duration of 55 minutes
print("Total seconds in 55 minutes:", totalsecond)

輸出:

Total seconds in 55 minutes:3300.0

範例2:在下麵的示例中,將在 total_seconds() 函數的幫助下根據第二個值返回 -3*13 分鍾。

Python3


# Python3 code for getting
# total seconds for the
# given duration of time
# Importing time and timedelta module
from datetime import time, timedelta 
     
# Specifying a time duration
A = timedelta(minutes = -3*13)
# Calling the total_seconds() function
# over the specified time duration
totalsecond = A.total_seconds()
# Getting the Total seconds for
# the duration of -3*13 minutes
print("Total seconds in -3*13 minutes:", totalsecond)

輸出:

Total seconds in -3*13 minutes:-2340.0

範例3:在下麵的示例中,total_seconds() 函數將“天 = 2、小時 = 3、分鍾 = 43、秒 = 35”的持續時間轉換為其等效的秒值。

Python3


# Python3 code for getting
# total seconds for the
# given duration of time
# Importing time and timedelta module
from datetime import time, timedelta 
     
# Specifying a time duration
A = timedelta(days = 2, hours = 3,
              minutes = 43,
              seconds = 35)
# Calling the total_seconds() function
# over the specified time duration
totalsecond = A.total_seconds()
# Getting the Total seconds
print("Total seconds are:", totalsecond)

輸出:

Total seconds are:186215.0




相關用法


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