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 trialEric Nachtsheim
2,006 PointsI'm stuck (Dates and Times in Python, Timedelta-minute)
I can't seem to figure out what I'm doing wrong. I think I don't understand timedelta well enough.
Below is what I've come up with, and the only error message I get from it is "Try again". I've tried a number of different variants, but I'm just baffled. Any help would be appreciated.
import datetime
def minutes(datetime1, datetime2):
datetime1 = datetime.datetime.now()
datetime2 = datetime.datetime.today()
seconds = datetime2.second - datetime1.second
total = datetime.timedelta.total_seconds(seconds)
minutes = round(total / 60)
return minutes
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsHey Eric Nachtsheim, you are not far off.
- The assignments to
datetime1
anddatetime2
create new local references that override the arguments passed in. Remove these assignments - A
datetime
object does have a seconds attribute. The refers to the number of partial minutes inrange(60)
- subtracting two
datetime
objects creates a timedelta object. so,seconds = datetime2 - datetime1
. "seconds" might not be the best variable name - It is this
timedelta
object that has thetotal_seconds()
method. sototal = seconds.total_seconds()
The rest flows correctly.
Post back if you need more help. Good luck!!
Eric Nachtsheim
2,006 PointsGot it. I see what I was doing wrong. Thanks so much!