Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Dates and Times Timedelta Minute

Dates and times in Python, challenge. Need some guidance with this task and also to help me understand what i'm missing.

All in all, this task has defeated me. My partner whom also writes in python could not even help me with this, which is why i'm coming here. So, I understand the concept of the datetime.timedelta() and all the others but i'm just not understanding how to execute what this task needs to move on. More specifically- "to get the number of seconds, returns the number of minutes, rounded, between them. The first will always be older and the second newer.". Id appreciate any help with this task.

Thank you, -Courtney

minutes.py
import datetime

def minutes(datetime1, datetime2):
    seconds = datetime1.timedelta.total_seconds()
    return minutes - seconds

2 Answers

Instead of datetime1.timedelta.total_seconds(), try using datetime.timedelta.total_seconds(). datetime.timedelta.total_seconds() needs a timedelta argument, which you can get by subtracting your first datetime parameter from your second datetime parameter. You would then have the difference between your datetime parameters in seconds, which you can convert into minutes by dividing by 60, since a minute has 60 seconds. Next, round the number of minutes and return that value. To round a number some_number, you can do round(some_number).

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Said another way, since the difference of two datetime objects returns a timedelta object, this difference can replace datetime.timedelta.

seconds = (datetime1 - datetime2).total_seconds()

Then use math to convert to minutes.

Thank you so much, this helped a lot!

Hi...