Home > database >  Calculate time between time in python and time in mysql database
Calculate time between time in python and time in mysql database

Time:12-06

`

import datetime
now = datetime.datetime.now ()
tgl = now.strftime("%Y-%m-%d")
y = now.strftime ('%H:%M:%S')
print(y)
>>> 20:01:10

crsr.execute("SELECT * FROM tblsolar WHERE tanggal LIKE '%s' AND id LIKE '1001' ;" % (tgl))
res = crsr.fetchall()
for i in res:
    z = i[9]
print(z)
>>>  8:09:34
can someone help me in this situation
i want to calculate between z and y (time duration)
x = y - z

any help thank you..

i have try `

import datetime as dt
start_dt = dt.datetime.strptime(x, '%H:%M:%S')
end_dt = dt.datetime.strptime(y, '%H:%M:%S')
diff = (end_dt - start_dt)
print(diff)

strptime() argument 1 must be str, not datetime.timedelta`

CodePudding user response:

I reckon that the %s placeholder should appear alone and outside single quotes:

sql = "SELECT * FROM tblsolar WHERE tanggal = %s AND id = '1001'"
crsr.execute(sql, (tgl,))
res = crsr.fetchall()

CodePudding user response:

you mean this SQL Syntax?

SELECT FORMAT(GETDATE(), 'HH:mm:ss') as DT

This should give you the same as the python time with format '%H%M%S'

  • Related