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

Honestly super lost on this one. Time Delta Hell!

Not sure where to go after defining "minutes". I will for sure have to review the "Dates and Times in Python" videos... not getting it

import datetime
def minutes(datetime.datetime(), datetime.datetime())

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 67,736 Points

Hey matthew mahoney, let’s see if we can break it down. The challenge asks:

  • Write a function named minutes that takes two datetimes

Using date1 and date2 as the parameters, the signature would look like:

def minutes(date1, date2):
  • use timedelta.total_seconds() to get the number of seconds between them

A timedelta object is automatically returned as the result of subtracting two datetime objects. Using the .total_seconds() method on this result will give the number of seconds the two datetime objects differ.

  • return the number of rounded minutes

Straightforward math here converting from seconds to minutes, then round off to an integer.

  • hint: The first will always be older and the second newer. You'll need to subtract the first from the second.

datetime objects relate to posix time (the member of seconds since 1/1/1970. Older times (farther in the past) are lower numbers since they’re closer to 1/1/1970.

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

Thanks! Brilliant help!

Chris Freeman
Chris Freeman
Treehouse Moderator 67,736 Points

If you get a negative result, keep in mind that the older date is lower value, so subtract the lower/earlier date from the higher/later date to get a positive result.

Or use your abs 💪! 🤣