import mysql.connector
import datetime
mydb = mysql.connector.connect(host="localhost", user="root", password="afif123", db="library")
mycursor = mydb.cursor()
date_query = '''SELECT Date from issued_book '''
mycursor.execute(date_query)
date_fetch = mycursor.fetchall()
date1 = date_fetch[0]
print(date1)
today = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(today)
diff = today - date1
print(diff)
我遇到这样的错误
(datetime.datetime(2019,4,18,15,47,51),)
回溯(最近通话最近): 2019-04-20 17:38:41
文件“ C:/Users/Desktop/lib/d.py”,第12行,在 diff =今天-date1
TypeError:-:“ str”和“ tuple”的不受支持的操作数类型
以退出代码1完成的过程
答案 0 :(得分:0)
例如,找到两个日期之间的差异仅需减去两个datetime对象即可。
Bytes
答案 1 :(得分:0)
好的,这对我有帮助
import mysql.connector
import datetime
mydb = mysql.connector.connect(host="localhost", user="root", password="afif123", db="library")
mycursor = mydb.cursor()
date_query = '''SELECT Date from issued_book '''
mycursor.execute(date_query)
date_fetch = mycursor.fetchall()
date1 = date_fetch[0]
print("date1:", date1)
d1 = date1[0]
print("a1:", d1)
today = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
t1 = datetime.datetime.strptime(today, "%Y-%m-%d %H:%M:%S")
print("today:", today)
diff = t1 - d1
print("diff:", diff)
输出 date1:(datetime.datetime(2019,4,18,15,47,51),)
a1:2019-04-18 15:47:51
今天:2019-04-21 12:00:47
差异:2天,20:12:56