ADVERTISEMENT
ADVERTISEMENT

Python timedelta total_seconds() Method with Example

Python timedelta.total_seconds() Method: Here, we are going to learn about the total_seconds() method of timedelta class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 04, 2020

Python timedelta.total_seconds() Method

timedelta.timedeltotal_seconds() method is used in the timedelta class of module datetime.

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.