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

Holden Glass
Holden Glass
6,077 Points

Code works in workspaces, not in challenge

I have run the code in workspaces multiple times and it works. I put it in the challenge and it no longer works. all it says is "Bummer! Try again!" I don't know what to do. any help will be appreciated.

minutes.py
import datetime

def minutes(date1, date2):
    seconds1 = date1.total_seconds()
    seconds2 = date2.total_seconds()

    difference = seconds2 - seconds1
    seconds = difference/60

    return round(seconds)

2 Answers

Steven Parker
Steven Parker
231,269 Points

:warning: Be careful about testing a challenge in workspaces or an external REPL.

If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.

I'm guessing that your code seemed to work because you were passing it timedelta arguments, but the challenge says the function should receive datetime arguments. You'll need to make a few changes to handle the correct type of argument.

Hint: Remember that subtracting one datetime from another will produce a timedelta.

Holden Glass
Holden Glass
6,077 Points

Ok. Thanks. This will be very helpful for future challenges. Thanks.

Holden Glass
Holden Glass
6,077 Points

also, quick question, do you know of a way to turn a datetime into a timedelta?

Steven Parker
Steven Parker
231,269 Points

I know a way to turn two datetimes into a timedelta (see my hint above).

But it doesn't make sense conceptually to convert just one - in what situation would you want to do that?

Holden Glass
Holden Glass
6,077 Points

I don't exactly know. I was thinking that I would turn both datetimes into timedeltas and then subtract and do the total_seconds.

Steven Parker
Steven Parker
231,269 Points

You have the right idea but the wrong order. What I was hinting at before is that if you subtract the first datetime from the second one, the result is a timedelta. Then you can take the total_seconds of that.

Holden Glass
Holden Glass
6,077 Points

I realized that after I completed the challenge another way.

Below worked for me

 from datetime import timedelta


def minutes(time_one, time_two):
    difference = timedelta.total_seconds(time_two - time_one)
    delta_time = round(difference/60)
    return delta_time