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
相关用法
- Python Pandas Timedelta.floor()用法及代码示例
- Python Pandas Timedelta.asm8用法及代码示例
- Python Pandas Timedelta.components用法及代码示例
- Python Pandas Timedelta.days用法及代码示例
- Python Pandas Timedelta.delta用法及代码示例
- Python Pandas Timedelta.seconds用法及代码示例
- Python Pandas Timedelta.nanoseconds用法及代码示例
- Python Pandas Timedelta.microseconds用法及代码示例
- Python Pandas Timedelta.ceil()用法及代码示例
- Python datetime.timedelta()用法及代码示例
- Python datetime.timetz()用法及代码示例
- Python datetime.utcoffset()用法及代码示例
- Python DateTime weekday()用法及代码示例
- Python datetime.tzname()用法及代码示例
- Java Vector removeElement()用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Python timedelta total_seconds() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。