Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I'm a Python newbie (2 weeks) and I'm having trouble formatting a datetime.timedelta object.

Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that duration in the format of hours: minutes.

I have tried a variety of methods for doing this and I'm having difficulty. My current approach is to add methods to the class for my objects that return hours and minutes. I can get the hours by dividing the timedelta.seconds by 3600 and rounding it. I'm having trouble with getting the remainder seconds and converting that to minutes.

By the way, I'm using Google AppEngine with Django Templates for presentation.

1 Answer

0 votes
by (106k points)

To format timedelta to string you can just convert the timedelta to a string with str(). Here's an example:-

import datetime 

start = datetime.datetime(2009,2,10,14,00) 

end = datetime.datetime(2009,2,10,16,00) 

delta = end-start print(str(delta)) 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 30, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...