skip to Main Content

`

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`

3

Answers


  1. Chosen as BEST ANSWER

    ok i got the answer :)

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

    thank @Tim Biegeleisen for correction the code thank NicoDorn for the input


  2. 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()
    
    Login or Signup to reply.
  3. 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’

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search