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

Devin Scheu
Devin Scheu
66,191 Points

Help with datetime function

Okay, I don't know what i'm doing wrong.

Question: Write a function named minutes that takes two datetimes and returns the number of minutes, rounded, between them. The first will always be older and the second newer. You'll need to subtract the first from the second.

minutes.py
import datetime
def minutes(datetime1, datetime2):
  difference = datetime2 - datetime1
  rounded = difference.round()
  return rounded

8 Answers

def minutes(dt1, dt2):
    seconds = dt2.timestamp() - dt1.timestamp()
    return round(seconds / 60)
BENOIT BELLONE
BENOIT BELLONE
12,418 Points

Hi guys, I guess this is rather import datetime def minutes(datetime1, datetime2): return round((datetime2-datetime1).seconds/60)

thank you for the help

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Devlin.

It is actually simpler than it appears you are thinking about it.

When you do the subtraction between the two datetimes (number2-number1 as u did), you get something like 00:10:00

Where 10 are the minutes. When I did the exercise I thought there was something built in the library to go from that to the minutes themselves, but I haven't found it so I have used .seconds to get the seconds and then divided by 60.

This is the code I wrote for this:

import datetime
def minutes(datetime1, datetime2):
  return (datetime2-datetime1).seconds/60

Hope that helps.

Vittorio

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

There's nothing to get minutes, only days, seconds, microseconds...

And you should have to use round() in there because the challenge expects an int, not a float (and division always produces a float in Python 3).

Devin Scheu
Devin Scheu
66,191 Points

Hey this seemed to work for me:

import datetime
def minutes(datetime1, datetime2):
  return int((datetime2-datetime1).seconds/60)
Devin Scheu
Devin Scheu
66,191 Points

It solves the float problem that Kenneth kindly pointed out :)

My code couldn't work until i considered to add 1 after the whole int function on return.

Guess it's because our python starts from 0, so it would give a second less than one to the required value.

LIKE THIS return int((datetime2-datetime1).seconds/60) +1

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You have to add the 1 because you're not rounding the result of the division like I told you to in the instructions.

Hahaha i just did

return round((date2-date1).seconds/60)

Thank you, i overlooked that part and thought i could get away with adding 1

Horacio Canales
Horacio Canales
3,852 Points

Hello! I am getting the minutes difference when I try my code in iPython, but the challenge keep saying:

Bummer! Got the wrong number of minutes. Expected 7, got -53.

This is my code:

import datetime

dt1 = datetime.datetime.now()
dt1m = dt1.replace(hour= 8, minute = 30)
dt2 = datetime.datetime.now()
dt2m = dt2.replace(hour= 10, minute = 45)

def minutes(dt1m, dt2m):
  res = round(dt2m.minute - dt1m.minute)
  return int(res) 

Am I missing something?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Consider this. I start with 5:55pm (1755) and 3:00am (0300). I subtract the minutes from each other and I get 55.

Are there only 55 minutes between those two times? What if they're days apart, too?

You need to get the minutes from the timedelta that you get from subtracting one datetime from another. timedelta doesn't have a minutes attribute, though, so you'll have to figure out how to make minutes from seconds.

Andrew Winkler
Andrew Winkler
37,739 Points

This is correct:

import datetime

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

import datetime dir(datetime)

Thats the correct answer i got and it passed