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

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

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 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 68,441 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 πŸ’ͺ! 🀣