S
S
SideWest2019-03-31 17:20:33
Python
SideWest, 2019-03-31 17:20:33

Python. Getting the difference in minutes, how to do?

I take information like:

2019-03-31 17:04:39.645493
from the database
Next, I need to find out how many minutes have passed since that moment and from now, here's what I built:
delta1 = datetime.datetime.now() - lf1  # lf1 это   2019-03-31 17:04:39.645493
        print(delta1.seconds())

On this I got
Exception has occurred: TypeError
unsupported operand type(s) for -: 'datetime.datetime' and 'str'

I understand that you can’t subtract a string from the datetime, but how then do you even count? What type is this date time?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SideWest, 2019-03-31
@SideWest

I found a solution for white people

now = datetime.datetime.now()
now = str(now)
now = datetime.datetime.strptime(now[0:19],'%Y-%m-%d %H:%M:%S')
lf1 = datetime.datetime.strptime(lf1[0:19],'%Y-%m-%d %H:%M:%S')
minutes_diff1 = round((now - lf1).total_seconds() / 60.0)

Displays the rounded difference in minutes

D
Denis Melnikov, 2019-03-31
@Mi11er

Getting the date from your string
bd = '2019-03-31 17:04:39.645493'
dt = datetime.datetime.strptime(bd[0:19],'%Y-%m-%d %H:%M:% S')
And then you already have

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question