#datetime

1 messages · Page 1 of 1 (latest)

primal coral
#

How do I calculate the minutes between times? I put different times in two columns in the format start = (datetime.datetime.now()).strftime("%H:%M:%S%d.%m.%Y")
end = (datetime.datetime.now()).strftime("%H:%M:%S%d.%m.%Y")and I need to find the difference in minutes, how can I do this?

crimson dew
#

pretty sure since their both time objects u can just sutract the 2

daring hinge
#

Probably best to not convert them to string right away.

def diff_in_minutes(start: datetime.datetime, end: datetime.datetime) -> float:
    return (end-start).total_seconds() / 60

Pass the start and end to a function and have it return the float value of minutes between the two times.

example:

import datetime


def diff_in_minutes(start: datetime.datetime, end: datetime.datetime) -> float:
    return (end-start).total_seconds() / 60



start = datetime.datetime.now()
end = datetime.datetime.now() + datetime.timedelta(minutes=5, seconds=40)

minutes = diff_in_minutes(start, end)
print(minutes)

# 5.666666666666667
primal coral
#

I am adding start & end to the database because they are in different classes and I need to add their difference to the database too

daring hinge
#

I'd argue that you don't need to store the difference at all. Store the start and end as timestamps, then calculate the difference when you need it.

primal coral
#

I'll try, thanks