michael
michael

Reputation: 110550

How can I format timedelta for display

My script calculate the difference in 2 time. Like this:

lasted = datetime.strptime(previous_time, FMT) - datetime.strptime(current_time, FMT)

It returns me a timedelta object. Currently, it gives me difference in seconds.

How can I format it for display nicely?

e.g. convert "121" to "00:02:01"?

Thank you.

Upvotes: 16

Views: 42211

Answers (6)

RossGK
RossGK

Reputation: 575

That fractional second bit is sometimes unwanted from a timedelta. A quick truncate of that fractional bit with a split and discard:

a = datetime.now()
b = datetime.now() - a

then

str(b).split('.')[0]

(assuming applications where fraction of a second is irrelevant to you)

Upvotes: 4

Sverrir Sigmundarson
Sverrir Sigmundarson

Reputation: 2631

Expanding on @blender's answer. In case you're interested in millisecond resolution

a = datetime.now()
b = datetime.now() - a
s = b.seconds
ms = int(b.microseconds / 1000)
'{:02}:{:02}:{:02}.{:03}'.format(s // 3600, s % 3600 // 60, s % 60, ms)

Upvotes: 0

Blender
Blender

Reputation: 298166

Have you tried using str()?

>>> a = datetime.now()                 
>>> b = datetime.now() - a
>>> b
datetime.timedelta(0, 6, 793600)
>>> str(b)
'0:00:06.793600'

Alternatively, you can use string formatting:

>>> a = datetime.now()                 
>>> b = datetime.now() - a
>>> s = b.total_seconds()
>>> '{:02}:{:02}:{:02}'.format(s // 3600, s % 3600 // 60, s % 60)
'00:00:06'

Upvotes: 23

frnhr
frnhr

Reputation: 12903

[insert shameless self-promotion disclamer here]

You can use https://github.com/frnhr/django_timedeltatemplatefilter

It's packaged as a tempalte filter for Django, so here is the important part, just plain Python:

def format_timedelta(value, time_format="{days} days, {hours2}:{minutes2}:{seconds2}"):

    if hasattr(value, 'seconds'):
        seconds = value.seconds + value.days * 24 * 3600
    else:
        seconds = int(value)

    seconds_total = seconds

    minutes = int(floor(seconds / 60))
    minutes_total = minutes
    seconds -= minutes * 60

    hours = int(floor(minutes / 60))
    hours_total = hours
    minutes -= hours * 60

    days = int(floor(hours / 24))
    days_total = days
    hours -= days * 24

    years = int(floor(days / 365))
    years_total = years
    days -= years * 365

    return time_format.format(**{
        'seconds': seconds,
        'seconds2': str(seconds).zfill(2),
        'minutes': minutes,
        'minutes2': str(minutes).zfill(2),
        'hours': hours,
        'hours2': str(hours).zfill(2),
        'days': days,
        'years': years,
        'seconds_total': seconds_total,
        'minutes_total': minutes_total,
        'hours_total': hours_total,
        'days_total': days_total,
        'years_total': years_total,
    })

Doesn't get more simple than that :) Still, check out the readme for a few examples.

For your example:

>>> format_timedelta(lasted, '{hours_total}:{minutes2}:{seconds2}')
0:02:01

Upvotes: 5

John La Rooy
John La Rooy

Reputation: 304147

You can truncate the seconds when using str by creating a new timedelta object

>>> a = datetime.now()
>>> b = datetime.now()
>>> c = b-a
>>> str(c)
'0:00:10.327705'
>>> str(timedelta(seconds=c.seconds))
'0:00:10'

Upvotes: 7

Chris Zheng
Chris Zheng

Reputation: 1499

Hope this fix your problem,

import datetime
start = datetime.datetime(2012,11,16,11,02,59)
end = datetime.datetime(2012,11,20,16,22,53)
delta = end-start
print ':'.join(str(delta).split(':')[:3])

In [29]: import datetime
In [30]: start = datetime.datetime(2012,11,16,11,02,59)
In [31]: end = datetime.datetime(2012,11,20,16,22,53)
In [32]: delta = end-start
In [33]: print ':'.join(str(delta).split(':')[:3])
4 days, 5:19:54

Upvotes: 1

Related Questions