Python-如何获取两个日期时间之间的秒数?

时间:2018-07-25 22:07:31

标签: python python-3.x datetime

我正在尝试获取当前时间与数据库中存在的时间戳(日期时间)之间的时间(以秒为单位),但是我似乎无法使其正常工作。.

config = json.loads(open('/var/www/html/config.json').read())
db = MySQLdb.connect(config['database']['host'],config['database']['user'],config['database']['password'],config['database']['dbname'] )
cursor = db.cursor()
sql = "select MANTIME from weather_settings"
cursor.execute(sql)
global delay
fetched = cursor.fetchone()
data = fetched[0]
cursor.close()
db.close()
dbtime = data
print("Current db time is: ",dbtime)
print("")
nowtime = time.strftime("%Y-%m-%d %H:%M:%S")
nowtime = datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
print("Current time is: ",nowtime)
print("")
difference = nowtime - dbtime
print("The difference is: ",difference)
print("")
if int(difference)/60 > 27:
    print("Time exceeds 27 minutes")
else:
    print("Time doesn't exceed 27 minutes") 

这给了我这种类型错误:TypeError:int()参数必须是字符串或数字,而不是'datetime.timedelta'

我似乎无法弄清楚,现在的代码可能不是很好。

有人可以帮助我获取当前日期和时间,并将其与数据库中日期时间的日期和时间进行比较,并返回它们之间的秒数吗?

0 个答案:

没有答案