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

Stuck in Timedelta Minutes Challenge

Hello,

I know this is a relatively easy challenge, but with great shame, I feel stuck.

I tried this code on eclipse and I feel that this is what the task is wanting me to do. However, I keep getting Bummer! Try again!

I am uncertain what I am failing to comprehend. Either what the task wants me to do or an error in my code I am not seeing it!

Please help! Thank you in advance.

minutes.py
import datetime

def minutes(datetime1, datetime2):
    datetime1 = datetime.timedelta.total_seconds(datetime1)
    datetime2 = datetime.timedelta.total_seconds(datetime2)
    difference_in_minutes = (datetime1 - datetime2) / 60
    return round(difference_in_minutes)

datetime1 = datetime.timedelta(seconds = 10)
datetime2 = datetime.timedelta(seconds = 59)

minutes(datetime1, datetime2)

2 Answers

Moosa Bonomali
Moosa Bonomali
6,297 Points

When we subtract the 2 dates, the result is a timedelta, so we only need to get the total_seconds from the resulting answer.

 import datetime

 def minutes(datetime1, datetime2):
     difference_in_minutes = (datetime2 - datetime1)
     return round(difference_in_minutes.total_seconds()/60)

 datetime1 = datetime.datetime.now() 
 datetime2 = datetime.datetime.now() + datetime.timedelta(seconds=1000)
 # print(datetime1)
 print(minutes(datetime1, datetime2))

Hello,

Thank you for the response! I tried to write it this way

import datetime

def minutes(datetime1, datetime2):
    difference_in_minutes = (datetime1 - datetime2).timedelta.total_seconds() / 60
    return round(difference_in_minutes)

and I got bummer try again.

When I tried it your way it worked!

So, I know the answer but I am still confused.

Why would it be wrong not to do timedelta.total_seconds() on the resulting answer. I honestly don't know the difference in doing timedelta.total_second() before doing the subtraction or doing it after subtraction. Aren't these two the same thing?

Moosa Bonomali
Moosa Bonomali
6,297 Points

The moment you subtract 2 datetime objects, the resulting object is a timedelta, So in you case the moment you do

(date1-date2)

the result if a timedelta and the only methods or attributes available after that are those that apply to timedelta. Doing this

(date1-date2).timedelta

will produce an error. so if you do this

print(type(date2-date1))

you will get this answer

<class 'datetime.timedelta'>

To conclude you only need to do this in your program. Since datetime1 is older you should do this instead

difference_in_minutes = (datetime2 - datetime1).total_seconds() / 60

to get a positive answer

I see. I just wanted to know the difference in your answer and my answer.

I thought even if I do .timedelta_total_seconds() before the subtraction it would not have mattered.

I was more interested in why it would give error rather than solving the task and you explained well.

Thank you!