Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timedelta to total seconds

I have a time difference

import time import datetime  time1 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())) ... time2 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())) diff = time2 - time1 

Now, how do I find the total number of seconds that passed? diff.seconds doesn't count days. I could do:

diff.seconds + diff.days * 24 * 3600 

Is there a builtin method for this?

like image 481
ripper234 Avatar asked Apr 02 '11 08:04

ripper234


People also ask

How do you convert Timedelta to seconds?

To get the Total seconds in the duration from the Timedelta object, use the timedelta. total_seconds() method.

What does Timedelta mean?

Timedeltas are differences in times, expressed in difference units, e.g. days, hours, minutes, seconds. They can be both positive and negative. Timedelta is a subclass of datetime.

How do you convert Timedelta to INT?

Convert the timedelta to int Using the dt Attribute in Pandas. To convert the timedelta to an integer value, we can use the pandas library's dt attribute. The dt attribute allows us to extract components of the timedelta . For example, we can extract the year, month, day, minutes, or seconds using the dt attribute.


1 Answers

Use timedelta.total_seconds().

>>> import datetime >>> datetime.timedelta(seconds=24*60*60).total_seconds() 86400.0 
like image 178
Andreas Jung Avatar answered Sep 30 '22 03:09

Andreas Jung