Python timedelta total_seconds() Method with Example

Python timedelta.total_seconds() Method: In this tutorial, we will learn about the total_seconds() method of timedelta class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 22, 2023

Python timedelta.total_seconds() Method

The timedelta.timedeltotal_seconds() method returns the total number of seconds covered in the given duration of that time instance.

Module

The following module is required to use total_seconds() method:

import datetime

Class

The following class is required to use total_seconds() method:

from datetime import timedelta

Syntax

The following is the syntax of total_seconds() method:

total_seconds()

Parameter(s)

The following are the 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 of timedelta total_seconds() Method in Python

## 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



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.