Open In App

Python timedelta total_seconds() Method with Example

The total_seconds() function is used to return the total number of seconds covered for the specified duration of time instance. This function is used in the timedelta class of module DateTime.

Syntax: total_seconds()



Parameters: This function does not accept any parameter.

Return values: This function returns the total number of seconds covered for the specified duration of time instance.



Example 1: In the below example, 55 minutes is going to be returned in terms of the second value with the help of total_seconds() function. 




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

Output:

Total seconds in 55 minutes: 3300.0

Example 2: In the below example, -3*13 minutes is going to be returned in terms of the second value with the help of total_seconds() function. 




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

Output:

Total seconds in -3*13 minutes: -2340.0

Example 3: In the below example, the total_seconds() function is converting the duration of “days = 2, hours = 3, minutes = 43, and seconds = 35” into its equivalent seconds value.




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

Output:

Total seconds are: 186215.0

Article Tags :