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 trialDevin Scheu
66,191 PointsHelp with datetime function
Okay, I don't know what i'm doing wrong.
Question: 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.
import datetime
def minutes(datetime1, datetime2):
difference = datetime2 - datetime1
rounded = difference.round()
return rounded
8 Answers
Lee Kiernan
Courses Plus Student 15,010 Pointsdef minutes(dt1, dt2):
seconds = dt2.timestamp() - dt1.timestamp()
return round(seconds / 60)
BENOIT BELLONE
12,418 PointsHi guys, I guess this is rather import datetime def minutes(datetime1, datetime2): return round((datetime2-datetime1).seconds/60)
Annie Scott
27,613 Pointsthank you for the help
Vittorio Somaschini
33,371 PointsHello Devlin.
It is actually simpler than it appears you are thinking about it.
When you do the subtraction between the two datetimes (number2-number1 as u did), you get something like 00:10:00
Where 10 are the minutes. When I did the exercise I thought there was something built in the library to go from that to the minutes themselves, but I haven't found it so I have used .seconds to get the seconds and then divided by 60.
This is the code I wrote for this:
import datetime
def minutes(datetime1, datetime2):
return (datetime2-datetime1).seconds/60
Hope that helps.
Vittorio
Kenneth Love
Treehouse Guest TeacherThere's nothing to get minutes, only days, seconds, microseconds...
And you should have to use round()
in there because the challenge expects an int, not a float (and division always produces a float in Python 3).
Devin Scheu
66,191 PointsHey this seemed to work for me:
import datetime
def minutes(datetime1, datetime2):
return int((datetime2-datetime1).seconds/60)
Devin Scheu
66,191 PointsIt solves the float problem that Kenneth kindly pointed out :)
MUZ140330 Simbarashe Chibgwe
2,825 PointsMy code couldn't work until i considered to add 1 after the whole int function on return.
Guess it's because our python starts from 0, so it would give a second less than one to the required value.
LIKE THIS return int((datetime2-datetime1).seconds/60) +1
Kenneth Love
Treehouse Guest TeacherYou have to add the 1 because you're not rounding the result of the division like I told you to in the instructions.
MUZ140330 Simbarashe Chibgwe
2,825 PointsHahaha i just did
return round((date2-date1).seconds/60)
Thank you, i overlooked that part and thought i could get away with adding 1
Horacio Canales
3,852 PointsHello! I am getting the minutes difference when I try my code in iPython, but the challenge keep saying:
Bummer! Got the wrong number of minutes. Expected 7, got -53.
This is my code:
import datetime
dt1 = datetime.datetime.now()
dt1m = dt1.replace(hour= 8, minute = 30)
dt2 = datetime.datetime.now()
dt2m = dt2.replace(hour= 10, minute = 45)
def minutes(dt1m, dt2m):
res = round(dt2m.minute - dt1m.minute)
return int(res)
Am I missing something?
Kenneth Love
Treehouse Guest TeacherConsider this. I start with 5:55pm (1755) and 3:00am (0300). I subtract the minutes from each other and I get 55.
Are there only 55 minutes between those two times? What if they're days apart, too?
You need to get the minutes from the timedelta
that you get from subtracting one datetime
from another. timedelta
doesn't have a minutes
attribute, though, so you'll have to figure out how to make minutes from seconds
.
Andrew Winkler
37,739 PointsThis is correct:
import datetime
def minutes(datetime1, datetime2):
return round((datetime2-datetime1).seconds/60)
MUZ141050 Ephraim Muwengwa
2,885 Pointsimport datetime dir(datetime)
Thats the correct answer i got and it passed