Python timedelta.total_seconds()方法 (Python timedelta.total_seconds() Method)
timedelta.timedeltotal_seconds() method is used in the timedelta class of module datetime.
timedelta.timedeltotal_seconds()方法在模块datetime的timedelta类中使用。
It uses an instance of the class and returns the total number of seconds covered in the given duration of that time instance.
它使用该类的实例,并返回在该时间实例的给定持续时间内覆盖的总秒数。
Module:
模块:
import datetime
Class:
类:
from datetime import timedelta
Syntax:
句法:
total_seconds()
Parameter(s):
参数:
None
没有
Return value:
返回值:
The return type of this method is a number which is the total number of seconds covered in that period.
此方法的返回类型是一个数字,该数字是该时间段内覆盖的总秒数。
Example:
例:
## 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()
Output
输出量
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
翻译自: https://www.includehelp.com/python/timedelta-total_seconds-method-with-example.aspx