Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times Timedelta Minute

Eric Nachtsheim
Eric Nachtsheim
1,993 Points

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.

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
Chris Freeman
Treehouse Moderator 67,464 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!!

Eric Nachtsheim
Eric Nachtsheim
1,993 Points

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