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

Sahar Nasiri
Sahar Nasiri
7,454 Points

What is wrong with this code?

minutes.py
import datetime

def minutes(t_1, t_2):
  return t_1.minute - t_2.minute

2 Answers

Steven Parker
Steven Parker
231,248 Points

:point_right: Here's a few hints:

  • Remember, the challenge said: "You'll need to subtract the first from the second."
  • When you subtract two datetimes, you get a timedelta. Timedeltas do not have a .minute property
  • Timedeltas do have a .second property.
  • Also remember the challenge said that the function: "...returns the number of minutes, rounded..."

Take these into account and I bet you can solve it.

Sahar Nasiri
Sahar Nasiri
7,454 Points

Please help me more! I cannot pass this challenge because I don't know why I can't use timedelta.minute when it is working?

Sahar Nasiri
Sahar Nasiri
7,454 Points

def minutes(t1, t2): t = t2 - t1 return t.year* 518400 + t.month*43200 + t.day*1440 + t.hour*60 + t.minute

Is it right? I don't know why I'm doing this :))

Sahar Nasiri
Sahar Nasiri
7,454 Points

I have tested my code in python shell too. timedelta does have a minute property. I don't still know what is wrong with my code?

Stefan Vaziri
Stefan Vaziri
17,453 Points

This worked for me:

import datetime

def minutes(dt1, dt2): difference = dt2 - dt1 second = difference.total_seconds() minutes = second/60 return round(minutes)