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 time.monotonic()用法及代码示例
- Python time.asctime()用法及代码示例
- Python time.clock()用法及代码示例
- Python time.ctime()用法及代码示例
- Python time.perf_counter()用法及代码示例
- Python time.get_clock_info()用法及代码示例
- Python time.gmtime()用法及代码示例
- Python time.time()用法及代码示例
- Python time.mktime()用法及代码示例
- Python time.sleep()用法及代码示例
- Python time.clock_gettime_ns()用法及代码示例
- Python time.process_time_ns()用法及代码示例
- Python time.monotonic_ns()用法及代码示例
- Python time.time_ns()用法及代码示例
- Python time.strftime()用法及代码示例
- Python time.clock_getres()用法及代码示例
- Python timeit()用法及代码示例
- Python time.clock_settime()用法及代码示例
- Python time.process_time()用法及代码示例
- Python time.localtime()用法及代码示例
注:本文由纯净天空筛选整理自 Python timedelta total_seconds() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。