Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times Timedelta Minute

What kind of datetimes are you asking me? Now and today?

Here is the task: Write a function named minutes that takes two datetimes and, using timedelta.total_seconds() to get the number of seconds, returns the number of minutes, rounded, between them. The first will always be older and the second newer. You'll need to subtract the first from the second. And I don't get anything with the time. Sadly it is in the end of the course so I can't finish this. I am giving up officially.

minutes.py
import datetime

def minutes(datetime_one, datetime_two):
    datetime_one = datetime.datetime.now()
    datetime_two = datetime.datetime.now()
    return int(timedelta.total_seconds(datetime_one - datetime_two) / 60)

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 29,142 Points

Hi Wicker. Good try. This was a difficult question. Few things going on here.

First the datetimes are being passed in so no need to set them to now(). They will have datetime values already. So delete these two lines:

datetime_one = datetime.datetime.now()
datetime_two = datetime.datetime.now()

Next instead of using the function int() you want round(). Because int() will do the same as floor() which is just remove the decimals instead of rounding up when it's greater than equal to .5.

Also timedelta.total_seconds should be datetime.timedelta.total_seconds.

Lastly the question states "The first will always be older and the second newer. You'll need to subtract the first from the second. " Little confusing but it should be datetime_two - datetime_one.

If you still can't get it to pass post your new code back to the Community and someone will help you out. Happy coding!

Thank you! I got this.