How do I convert datetime.timedelta to minutes, hours in Python?
There's no built-in formatter for timedelta
objects, but it's pretty easy to do it yourself:
days, seconds = duration.days, duration.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
Or, equivalently, if you're in Python 2.7+ or 3.2+:seconds = duration.total_seconds()
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
Now you can print it however you want:'{} minutes, {} hours'.format(minutes, hours)
For example:def convert_timedelta(duration):
days, seconds = duration.days, duration.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = (seconds % 60)
return hours, minutes, seconds
td = datetime.timedelta(2, 7743, 12345)
hours, minutes, seconds = convert_timedelta(td)
print '{} minutes, {} hours'.format(minutes, hours)
This will print:9 minutes, 50 hours
If you want to get "10 minutes, 1 hour" instead of "10 minutes, 1 hours", you need to do that manually too:print '{} minute{}, {} hour{}'.format(minutes, 's' if minutes != 1 else '',
hours, 's' if minutes != 1 else '')
Or you may want to write an english_plural
function to do the 's'
bits for you, instead of repeating yourself.From your comments, it sounds like you actually want to keep the days separate. That's even easier:
def convert_timedelta(duration):
days, seconds = duration.days, duration.seconds
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = (seconds % 60)
return days, hours, minutes, seconds
If you want to convert this to a single value to store in a database, then convert that single value back to format it, do this:def dhms_to_seconds(days, hours, minutes, seconds):
return (((days * 24) + hours) * 60 + minutes) * 60 + seconds
def seconds_to_dhms(seconds):
days = seconds // (3600 * 24)
hours = (seconds // 3600) % 24
minutes = (seconds // 60) % 60
seconds = seconds % 60
return days, hours, minutes, seconds
So, putting it together:def store_timedelta_in_database(thingy, duration):
seconds = dhms_to_seconds(*convert_timedelta(duration))
db.execute('INSERT INTO foo (thingy, duration) VALUES (?, ?)',
thingy, seconds)
db.commit()
def print_timedelta_from_database(thingy):
cur = db.execute('SELECT duration FROM foo WHERE thingy = ?', thingy)
seconds = int(cur.fetchone()[0])
days, hours, minutes, seconds = seconds_to_dhms(seconds)
print '{} took {} minutes, {} hours, {} days'.format(thingy, minutes, hours, days)
Convert a timedelta to days, hours and minutes
If you have a datetime.timedelta
value td
, td.days
already gives you the "days" you want. timedelta
values keep fraction-of-day as seconds (not directly hours or minutes) so you'll indeed have to perform "nauseatingly simple mathematics", e.g.:
def days_hours_minutes(td):
return td.days, td.seconds//3600, (td.seconds//60)%60
Convert datetime.timedelta to minutes
Take the total_seconds
and divide by 60.
>>> import datetime
>>> td = datetime.timedelta(seconds=90)
>>> td.total_seconds() / 60
1.5
How to get total hours and minutes for timedelta in Python
You can use total_seconds()
to compute the number of seconds. This can then be turned into minutes or hours:
>>> datetime.timedelta(days=3).total_seconds()
259200.0
Is there a way to format a timedelta object to be hours-minutes-seconds.MILLISECONDS?
Given your initial code example, you could use something like this:
# From your example.
c = b - a
# Get the hours, minutes, and seconds.
minutes, seconds = divmod(c.seconds, 60)
hours, minutes = divmod(minutes, 60)
# Round the microseconds to millis.
millis = round(c.microseconds/1000, 0)
print(f"Doing <something> took {hours}:{minutes:02}:{seconds:02}.{millis}")
which results in# c = datetime.timedelta(seconds=7, microseconds=319673)
Doing <something> took 0:00:07.320
Take a look at Python’s built-in functions round() and divmod() and please poke around this and this related thread; also, please read through this and this thread to learn more about formatting timedelta objects. How do I convert seconds to hours, minutes and seconds?
You can use datetime.timedelta
function:
>>> import datetime
>>> str(datetime.timedelta(seconds=666))
'0:11:06'
Related Topics
What Are All the Dtypes That Pandas Recognizes
Syntax Error: Invalid Syntax' for No Apparent Reason
Use Index in Pandas to Plot Data
Multiprocessing.Pool Makes Numpy Matrix Multiplication Slower
Changing the Options of a Optionmenu When Clicking a Button
Is It Better to Use "Is" or "==" for Number Comparison in Python
How to Display Utf-8 in Windows Console
Fastest Way to Sort Each Row in a Pandas Dataframe
Python, Default Keyword Arguments After Variable Length Positional Arguments
Django: How to Build a Custom Form Widget
Why Isn't Assigning to an Empty List (E.G. [] = "") an Error
Suppressing Scientific Notation in Pandas
Animated Subplots Using Matplotlib
How to Align Gridlines for Two Y-Axis Scales Using Matplotlib
Asyncio Cancellederror and Keyboardinterrupt