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

Keerthi Suria Kumar Arumugam
Keerthi Suria Kumar Arumugam
4,585 Points

Challenge Task 1 of 1 : Help needed.

Can someone tell me what is that I am doing wrong in the code?

The error message is : "Expected 7, got 1433"

minutes.py
import datetime

def minutes(datetime1,datetime2):
  temp = datetime1 - datetime2
  return round(temp.seconds/60)

1 Answer

Frederick Pearce
Frederick Pearce
10,677 Points

You are calculating the minutes based only on the seconds between datetime1 and datetime2, not the total time between them (i.e. you're neglecting the other instance attributes: days and microseconds). One alternative is to use the method that outputs total number of seconds (total_seconds()), such that

import datetime

def minutes(dt1, dt2):
    return round((dt2 - dt1).total_seconds()/60)
Keerthi Suria Kumar Arumugam
Keerthi Suria Kumar Arumugam
4,585 Points

I was doing the other way round. dt1 - dt2 instead of dt2 - dt1. Anyway, your answer is the better one. Since it takes into account any hours, minutes and seconds. Thank you.