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

Wrong number of Minutes?

The code gives the feedback "Got the wrong number of minutes. Expected 7, got -53> Write a function named minutes that takes two datetimes and 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. Whats up?

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

3 Answers

akak
akak
29,445 Points

Let's assume time1 = (2015, 9, 18, 21, 32, 34, 989839) and time2 = (2015, 9, 18, 22, 16, 4, 442252). This is how datetime.datetime object looks like. When you do time2.minutes minus time1.minutes it takes 16 (as this is the value of minutes in 2nd object) and subtracts 32 (value of minutes in the first object) which gives -16. Well that's not a correct difference.

But when you do time2-time1 you get timedelta objects that equals (0, 2609, 452413). 0 days, 2609 seconds and 452413 microseconds have passed between those two times. Now if you format it taking seconds and dividing by 60 you get 43 minutes between those two times. Which is correct.

Hope that helps :)

akak
akak
29,445 Points

Hi, UPDATE: I read that wrong. But you may try this :)

 def minutes(time1, time2):
  return round((time2 - time1).seconds/60)
James N
James N
17,864 Points

this isn't working for me. when i try the above code, i get :

Traceback (most recent call last):                                               
  File "Treehouse.py", line 5, in <module>                                       
    print(minutes(datetime.datetime.now().month , datetime.datetime.today()))    
  File "Treehouse.py", line 3, in minutes                                        
    return round((datetime2, datetime1).second/60)                               
AttributeError: 'tuple' object has no attribute 'second' 

if i remove the brackets, surrounding the time2 and time1 variables, i get:

Traceback (most recent call last):                                               
  File "Treehouse.py", line 5, in <module>                                       
    print(minutes(datetime.datetime.now().month , datetime.datetime.today()))    
  File "Treehouse.py", line 3, in minutes                                        
    return round(datetime2, datetime1.second/60)                                 
AttributeError: 'int' object has no attribute 'second' 

Can someone help me here? Thanks.

James N
James N
17,864 Points

EDIT: I just tried copying and pasting your code and it works fine.

my code was:

import datetime
def minutes(datetime1, datetime2):
  return round(datetime2, datetime1.second/60) 

But i don't understand why you put brackets around time2 and time1.

Can someone explain the Nitty Gritty of it all?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

James, it looks like you have a typo. There should be a minus sign (-) not a comma in the return line:

return round((datetime2, datetime1).second/60)                               

Change to

return round((datetime2 - datetime1).second/60)                               
James N
James N
17,864 Points

Whoops! Thanks!

Thanks, Why did that way work and not the code I used?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Benjamin, your code subtracted the minute values without regard to the difference in the hours. In Akak's solution, subtracting two datetime objects creates a timedelta object that has the absolute difference. Then it's a matter of converting the units as needed.