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

sidni ahmed
sidni ahmed
3,329 Points

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

how would this challenge be done?

i came across the following code below.

when i run this code i will get the output '0', is this correct?

what is this function in this challenge supposed to do? what is its use?

minutes.py
import datetime

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

5 Answers

Hi sidni,

If dt1 and dt2 are different datetimes, then the function should return the difference between the two datetimes. For example, if dt1 is the start of your shift at work or your day at school, and dt2 is the end, you'd be able to tell how long you were at the institution using that function.

Hope that helps!

sidni ahmed
sidni ahmed
3,329 Points

how would you use this function?

this is what i did:

minutes.py
old = datetime.datetime(2016, 1, 11, 12, 30, 0)

new = datetime.datetime.now()
#>>> new
datetime.datetime(2016, 1, 11, 15, 29, 29, 164906)

#>>> minutes(new, old)
-179


. . .

# i think the 'old' and 'new' variables are holding too much information. maybe i need the function to refer to just the hours and minutes

That's exactly how you'd use the function.

Practically though, you'd run datetime.datetime.now() at the start of an event and assign it to a variable, then again at the end of the end of the event, and use minutes() to determine how long it took the user or the host running the code to perform an action.

sidni ahmed
sidni ahmed
3,329 Points

can you give me an example please? it would really help. Thanks

import datetime

now = datetime.datetime.now()

print
print "Current date and time using str method of datetime object:"
print str(now)

print
print "Current date and time using instance attributes:"
print "Current year: %d" % now.year
print "Current month: %d" % now.month
print "Current day: %d" % now.day
print "Current hour: %d" % now.hour
print "Current minute: %d" % now.minute
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond

print
print "Current date and time using strftime:"
print now.strftime("%Y-%m-%d %H:%M")

print
print "Current date and time using isoformat:"
print now.isoformat()
sidni ahmed
sidni ahmed
3,329 Points

i feel bad asking so many questions. Thanks for your in depth reply. I was not clear in my previous question. What i meant was if you could give me an example in python of how this function 'minutes' will be used. When i use it i get negative numbers in the interpreter. i really appreciate the help. Sorry if i'm bothering you :)

Ah, I understand. You could use minutes() with a small modification in this way;

import datetime

def timeconsumer():
    now = datetime.datetime.now()
    L2 = []
    for i in range(99999):
        L2.append(i)
    then = datetime.datetime.now()
    print(minutes(now, then))

def minutes(dt1, dt2):
    change = dt2 - dt1
    seconds = change.total_seconds()
    return seconds

timeconsumer()

The modification is because this function doesn't take minutes to execute, so it would always return zero if the step to convert the result into minutes was retained. In this way, it's being used similarly to a Python library called timeit.

sidni ahmed
sidni ahmed
3,329 Points

thank you Evan :)