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

minutes.py challenge

Please help me with this challenge

minutes.py
import datetime


def minutes(t1, t2):
  t1 = datetime.datetime.now()
  t2 = datetime.datetime.now()
  deff = datetime.timedelta(t2-t1)
  return round(deff.minute)

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You don't make a timedelta with math like that. The act of doing math with two datetimes will make the timedelta directly (kinda like how 2 + 2 makes an int directly). So, change your deff line for that.

And, secondly, timedeltas don't have a minute attribute. They have a seconds attribute, though, so you'll need to turn seconds into minutes before you do the rounding.

I had issues with this code challenge as well. After some searching I came across a solution that worked for me:

import datetime
def minutes(datetime1, datetime2):
  return int((datetime2-datetime1).seconds/60)

the int() is there because it returns a float without making it into an integer (5.0 instead of 5 minutes)

Hope this helps! If you need help explaining this just let me know!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hey, thanks for pointing that it. This challenge shouldn't be solvable with int() so I've gone and corrected it.

Since I used that method to solve it, what exactly should the solution be? I couldn't seem to find another way around it.

stick round instead of int in there and works just fine ;)