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

Time-Stuck...

Can someone please help me with this challenge? I can't seem to make head-ways with this no matter how I try it...

minutes.py
import datetime


time_record_1 = datetime.datetime.today() - datetime.timedelta(hours=2)
time_record_2 = datetime.datetime.today()

def minutes(time_record_1, time_record_2):
    time_diff = time_record_2 - time_record_1
    print(datetime.timedelta.total_seconds(time_diff))

1 Answer

Steven Parker
Steven Parker
221,297 Points

:point_right: It looks like you have three issues:

  • The challenge asked for the result in minutes, not seconds (you'll need to convert)
  • The challenge also asked for the result to be rounded
  • You need to return the result, not print it

Also, while it doesn't hurt anything, the two variables you defined above the function are not used (and not necessary).

Hi Steven. Thank you for helping out.

I attempted to convert the seconds to minutes by dividing the hours by dividing the seconds by 60, rounding by using the .round() function, and replacing print() with return. However I do not get the right answer. Do you have any other tips to improve this?

Thank you

import datetime


time_record_1 = datetime.datetime.today() - datetime.timedelta(days=63)
time_record_2 = datetime.datetime.today()

def minutes(time_record_1, time_record_2):
    time_diff = time_record_2 - time_record_1
    time_diff_min = (datetime.timedelta.total_seconds(time_diff)) / 60
    return time_diff_min.round(0)