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

Ava Jones
Ava Jones
2,662 Points

The video did not give me much information about this, can someone please explain this to me

I do not know why this video does not give much info about the challenge

import datetime

def minutes (day1, day2):
    timedelta = day2 - day1

1 Answer

Asher Orr
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,086 Points

Hi Ava!

The directions say:

"Write a function named minutes that takes two datetimes and, using timedelta.total_seconds() to get the number of seconds, returns the number of minutes, rounded, between them. The first will always be older and the second newer. You'll need to subtract the first from the second."

To break it down simply, the challenge wants a function that finds the number of minutes between two datetime objects.

Here's another way to think about it: "I have one datetime object: 3 pm on August 1st. I have another datetime object: 5 pm on August 3rd. How many minutes passed from 3 pm on August 1st to 5 pm on August 3rd?

Your code is doing this:

import datetime
#import datetime module

def minutes (day1, day2):
#define the function "minutes", which takes 2 datetime objects.
    timedelta = day2 - day1
    #make an object called timedelta, which represents the gap between datetime object 2 and datetime object 1.

Nice work so far.

Now, you need to:

  • find the number of seconds in your object "timedelta"
new_object = variable_name.total_seconds()
#in this case, your variable name is "timedelta"
  • divide that number by 60 (to get the number of minutes.)
new_object_in_minutes = new_object/60
  • return the rounded number of minutes.
return round(new_object_in_minutes)

I hope this helps!