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

Andrei Oprescu
Andrei Oprescu
9,547 Points

What am I supposed to do here?

I am confused because the wording is weird. can you help me solve this code?

import datetime

def minutes(date, date1): timedelta.total_seconds(date, date1)

Please look at the question of the quiz and tell me what I am supposed to do.

Thanks

minutes.py
import datetime

def minutes(date, date1):
    timedelta.total_seconds(date, date1)
David Dzsotjan
David Dzsotjan
5,929 Points

I can't see the question, but what timedelta.total_seconds() does is return the total number of seconds contained in the duration. If you need to calculate the number of minutes elapsed between date and date1, then you just need to divide the result by 60 and take the integer part of it. Hope this helps.

Andrei Oprescu
Andrei Oprescu
9,547 Points

My question was:

Write a function named minutes that takes two datetimes and, using timedelta.total_seconds() to get the number of seconds, 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.

Hope you can help me.

Thanks

2 Answers

David Dzsotjan
David Dzsotjan
5,929 Points

Okay, then you got to define a variable that gets the returned value from timedelta.total_seconds(). This will be the number of seconds elapsed between date and date1. Then, you can define another variable, say, "minutes" that gets the value of int(seconds/60). Then, you return the variable minutes. Does this help?

David Dzsotjan
David Dzsotjan
5,929 Points

So, for example

import datetime

def minutes(date, date1):

    duration = date1 - date  # Create a timedelta object
    seconds = duration.total_seconds()  # Seconds elapsed between date1 and date
    minutes = int(seconds / 60)

    return minutes
Andrei Oprescu
Andrei Oprescu
9,547 Points

Thank you! Your answer helped a lot!

David Dzsotjan
David Dzsotjan
5,929 Points

You're welcome, glad I could help :)