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

I'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.

minutes.py
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
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hey Eric Nachtsheim, you are not far off.

  • The assignments to datetime1 and datetime2 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 in range(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 the total_seconds() method. so total = seconds.total_seconds()

The rest flows correctly.

Post back if you need more help. Good luck!!

Got it. I see what I was doing wrong. Thanks so much!