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


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