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

Tyler Cortez
Tyler Cortez
2,420 Points

write a function named minutes..timedelta

little confused here none of the videos I've seen have touched on how to do this. ty

minutes.py
import datetime

def minutes (day1, day2):
    timedelta = day2 - day1
    return abs(timedelta.total_seconds())

2 Answers

Your function currently returns the number of seconds. Try converting the seconds to minutes, then rounding the number of minutes by using the round function. For example, round(0.9) gives the result 1.

Using the abs function is unnecessary in this case, since the challenge specifies that the first parameter refers to the older datetime.

Andy Hughes
Andy Hughes
7,198 Points

Hey there, I also got stuck on this.

Looking at yours, you are good up to the return. Here's the steps I used in the exercise:

  1. Import datetime - which you've done
  2. Create the function with two dates - which you've done
  3. Create a timedelta using the two dates - which you've done
  4. Get total seconds and store it for use - Think about how you stored your timedelta. Can you do the same for total seconds?
  5. Convert total seconds into minutes. This is a simple case of storing the output from a maths equations to convert seconds to minutes.
  6. Round up your mins - Using your stored mins, this becomes a simple 'round' call on the mins you stored.

Hope that's of use. (I suspect I'm too late and you've long since completed it.) :)