Python Dates and Times in Python Dates and Times Timedelta Minute

Unashe Mutambashora
Unashe Mutambashora
2,483 Points

Converting seconds obtained from timedelta.total_seconds() into minutes

Hie guys. How do I do that. Cause It seems I have satisfied all the other requirements

minutes.py
from datetime import timedelta
from datetime import datetime


def minutes(time1, time2):
    if time2 > time1:
        time_difference = timedelta(minutes=time2) - timedelta(minutes=time1)
        return time_difference.total_seconds() / 60
    else:
        print("time2 has to be greater than time1")

3 Answers

Phil Livermore
Phil Livermore
11,016 Points

Your time difference isn't calulating correctly. You don't need timedelta, you just need to subtract time1 from time2.

You also need to round to the nearest minute.

Unashe Mutambashora
Unashe Mutambashora
2,483 Points

Thanks Phil for the response. So calling the timedelta class is not always required when trying to add/subtract time?

Phil Livermore
Phil Livermore
11,016 Points

Not if you already have two datetimes. If you want to subtract/add a certain amount of time, say 1 day or 2 hours than you can use timedelta but as the values are already datetimes it is not necessary. I think the question is a bit misleading as it mentions timedelta, but I think they are hinting that you create a variable called timedelta, which you have called time_difference instead.