我正在使用python 2.7.2。我试图得到两个日期变量之间的差异,这是由MySQL查询导致的。如何获得python中两个日期变量之间的小时差异
#!/usr/local/python2.7.2/bin/python
import sys
import datetime
from datetime import datetime
import MySQLdb
import MySQLdb.cursors
# Inventory db connection
db1=MySQLdb.connect(host = "localhost",user = "root",passwd ="*****",db="inventory")
cursor=db1.cursor()
cursor.execute("select max(date_created) from inventory.inventory_details;")
inv=cursor.fetchall()
# Remote Inventory db connection
db2=MySQLdb.connect(host = "remotehost",user = "root",passwd ="*****",db="inventory")
cursor=db2.cursor()
cursor.execute("select max(date_created) from inventory.inventory_details;")
remote_inv=cursor.fetchall()
print inv
print remote_inv
d1 = str(inv)
d2 = str(remote_inv)
diff = d2-d1
diff_minutes = diff.seconds/60
这里的 “INV” 输出为((datetime.datetime(2014,11,20,13,54,7),),)remote_inv的 输出为((datetime.datetime(2014年,11 ,20,19,17,2),),)
我需要转换为格式“%Y-%m-%d%H:%M:%S”。我尝试使用str()并没有帮助。
只是帮助我得到两个MySQL日期变量的区别..我只是想知道这是否可以在python中执行。
在此先感谢。
diff.total_seconds()'。我不明白格式是如何获得小时 – 2014-11-21 09:39:48