我正在减去两个日期和计算以小时为单位的差异蟒蛇比较两个日期
c_date = "2011-03-23 12:52:14"
c_obj_date = datetime.datetime.strptime(c_date, "%Y-%m-%d %H:%M:%S")
n_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print "Creation Date:", c_date
print "Current Date: ", n_date
hours = abs(n_date - c_obj_date).total_seconds()/3600.0
print hours
但是这给了我这个错误:
File "./eight.py", line 69, in zabbix_result
hours = abs(n_date - c_obj_date).total_seconds()/3600.0
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.datetime'
我使用的python2.4
UPDATE :
我现在使用
n_date = datetime.datetime.now()
但total_seconds()不会在python2.4中工作?任何想法如何我可以减去两个日期时间对象,并获得小时差异?
看到我的回答以下... – thebjorn 2012-04-07 18:05:19