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

David Fry
David Fry
13,267 Points

Datetimes stage 1: challenge Task 1 of 1 -- Not sure what I am doing wrong?

Here is the task: Write a function named minutes that takes two datetimes and 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.

Here is my code: import datetime

def minutes(time1, time2): return time1.minute - time2.minute

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

So, think about this. A datetime is a specific point in time. If I give you a datetime for yesterday at 1pm, and a datetime for today at 1:30pm, if you just subtract the minutes from each other, you'll get 30. Is there only 30 minutes between those two datetimes?

When you subtract one datetime from another, you get a timedelta, which represents a duration of time. It doesn't have a minutes attribute but it does have seconds. How can you go from seconds to minutes?

David Fry
David Fry
13,267 Points

I got it thanks! It was one of those smack your forehead moments.

Thanks Kenneth! This explanation really helped a lot.