当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。