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
Saghir Hussain
8,765 PointsHow do I work out the difference in the number of minutes between two datetimes
Hi,
I'm stuck on this task in the datetime module.
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.
My code is: def minutes(arg1, arg2): diff = arg1.minute - arg2.minute return round(diff)
Unfortunately the response is ' was expecting 7 but got 53'. I have no idea what I'm doing wrong. Can someone please help me - been stuck on this for several days.
1 Answer
Michael Vandenburg
6,952 PointsAccording to the instructions for that challenge, the first argument is before the second, so you need to subtract arg1 from arg2. But when I tried that using your code, I got -53, which I should have expected, since that's what happens when you reverse the order of operands in subtraction. So what else is going on?
It helped me to think about what might be being passed in as arguments. We know that it expects 7, so it could be that the times are 2:03 and 2:10, but whether you subtract 3 from 10 or 10 from 3, it's not 53. They must be something like 2:55 and 3:02. The minute components of those have a difference of 53, but the difference between the two times is just 7 minutes because the one with the lower minute number is in the next hour.
Put another way, getting the minute value of each argument is dropping the digits of the subtraction problem to the left of the minute, even though you need to keep them until you've done the math. The good news is that you can subtract two datetime.datetime objects and get a datetime.timedelta. Remember that that won't give you minutes, though, so you still have to do the math.