i am subtracting two dates and computing the difference in hours
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
however this gives me this error:
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'
i am using python2.4
UPDATE:
i now used
n_date = datetime.datetime.now()
but the total_seconds() wont work in python2.4?? any idea how i can subtract two datetime object and get the hour difference?
n_date is a string and c_obj_date is a datetime.datetime object. I’m not sure where you’re getting the .total_seconds() method from either (it’s not a timedelta member…). Try something like this: