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

Write a function named minutes that takes two datetimes and returns the number of minutes, rounde

i need to know what is wrong on it

minutes.py
import datetime

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

2 Answers

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

You have to use the time1 and time2 you defined as inputs in your function. Then you have to substract them, this will return a timedelta. This timedelta has seconds but not minutes, so you'll have to divide by 60 and round it.

import datetime

def minutes(time1, time2):
  time_delta = time2 - time1
  seconds = time_delta.seconds
  minutes = round(seconds/60)
  return minutes
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Wael,

The first issue I noticed you aren't using the passed in parameters time1 and time2. Let's step through migrating your code to a working solution

# Your original code:
def minutes(time1, time2):
    return datetime.minutes - datetime.minutes

# Change return statement to use the function arguments
  return time2 - time1 #returns time_delta object

# Change to return seconds
  return (time2 - time1).seconds

# Convert to minutes
  return (time2 - time1),seconds/60

# Round result to nearest minute to give final results
def minutes(time1, time2):
  return round((time2 - time1).seconds/60)