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

PYTHON DATES AND TIMES

not sure why it says got 55 expected 5. I ran the function locally and it works. if i did time1=datetime.datetime.now() and time2=datetime.datetime.now()+datetime.timedelta(minutes=+10)

and when i pass the values to my function minutes(time2,time1) it return 10 which is correct.

not sure why the code challenge shows 55.

minutes.py
import datetime
def minutes(time2,time1):
  return round(time2.minute - time1.minute)

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

The first datetime that comes to your function will always be older than the second one. Think of it like def minutes(older, newer):. Older datetimes should be considered "smaller" than newer ones (since dates get bigger as they go along). So you'll need to subtract the first one from the second one, newer - older, to get the right answer.

thanks Ken

although it still says try again.

import datetime
def minutes(older,newer):
  return round(newer.minute - older.minute)

this works fine locally when i pass values like older=datetime.datetime.now() and newer=now+datetime.timedelta(minutes=+5) which return 5 which is correct.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You're assuming that the difference in their minutes is the same as the number of minutes between them. Consider 1:05pm and 1:00am. The difference in their minutes is 5 but the difference between them, in minutes, is (12*60)-5, which is a lot more than 5.

You need to subtract the older datetime from the newer one and then round() the minutes attribute of the timedelta that you get from that subtraction.