📜  Python timedelta total_seconds() 方法与示例

📅  最后修改于: 2022-05-13 01:54:49.610000             🧑  作者: Mango

Python timedelta total_seconds() 方法与示例

total_seconds()函数用于返回指定的时间实例持续时间所涵盖的总秒数。该函数用于 DateTime 模块的 timedelta 类。

示例 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)


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)


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 in 55 minutes: 3300.0

示例 2:在下面的示例中,将在 total_seconds()函数的帮助下根据第二个值返回 -3*13 分钟。

蟒蛇3

# 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”的持续时间转换为其等效的秒值。

蟒蛇3

# 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