How do I remove the microseconds from a timedelta object?

PythonDatetimeFormatTimedelta

Python Problem Overview


I do a calculation of average time, and I would like to display the resulted average without microseconds.

avg = sum(datetimes, datetime.timedelta(0)) / len(datetimes)

Python Solutions


Solution 1 - Python

If it is just for the display, this idea works :

avgString = str(avg).split(".")[0]

The idea is to take only what is before the point. It will return 01:23:45 for 01:23:45.1235

Solution 2 - Python

Take the timedelta and remove its own microseconds, as microseconds and read-only attribute:

avg = sum(datetimes, datetime.timedelta(0)) / len(datetimes)
avg = avg - datetime.timedelta(microseconds=avg.microseconds)

You can make your own little function if it is a recurring need:

import datetime

def chop_microseconds(delta):
    return delta - datetime.timedelta(microseconds=delta.microseconds)

I have not found a better solution.

Solution 3 - Python

another option, given timedelta you can do:

avg = datetime.timedelta(seconds=math.ceil(avg.total_seconds()))

You can replace the math.ceil(), with math.round() or math.floor(), depending on the situation.

Solution 4 - Python

c -= timedelta(microseconds=c.microseconds)

Solution 5 - Python

Given that timedelta only stores days, seconds and microseconds internally, you can construct a new timedelta without microseconds:

from datetime import timedelta
datetimes = (timedelta(hours=1, minutes=5, seconds=30, microseconds=9999), 
             timedelta(minutes=35, seconds=17, microseconds=55), 
             timedelta(hours=2, minutes=17, seconds=3, microseconds=1234))
avg = sum(datetimes, timedelta(0)) / len(datetimes) # timedelta(0, 4756, 670429)
avg = timedelta(days=avg.days, seconds=avg.seconds) # timedelta(0, 4756)

Solution 6 - Python

I think this is the best way to get the last minutes without microseconds:

import time
import datetime
NOW = datetime.datetime.now()
LASTMINUTE =  NOW - datetime.timedelta(minutes=1, seconds = NOW.second, microseconds = NOW.microsecond)

Solution 7 - Python

I found this to work for me:

start_time = datetime.now()
some_work()
end time = datetime.now()
print str(end_time - start_time)[:-3]

Output:

0:00:01.955

I thought about this after searching in https://docs.python.org/3.2/library/datetime.html#timedelta-objects

Hope this helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHobbestigrouView Question on Stackoverflow
Solution 1 - PythonsangorysView Answer on Stackoverflow
Solution 2 - PythonHobbestigrouView Answer on Stackoverflow
Solution 3 - PythoneranView Answer on Stackoverflow
Solution 4 - PythonRafalView Answer on Stackoverflow
Solution 5 - Pythonldav1sView Answer on Stackoverflow
Solution 6 - PythonKris LukacsView Answer on Stackoverflow
Solution 7 - PythonEitan.k.View Answer on Stackoverflow