Subtract SQL DATETIME from datetime.now() in Python
Question
I have a DATETIME field in SQL. Its content is: 2012-08-26 13:00:00
I want to know how much time has passed from that date until now.
In Python 2.7, it's easy:
import time,datetime
start = datetime.datetime.strptime('2012-08-26 13:00:00', '%Y-%m-%d %H:%M:%S')
end = datetime.datetime.now()
delta = start - end
print delta
But I have a web server running Python 2.4. In Python 2.4 strptime is not in the datetime module. I can't figure out how to accomplish the same thing in 2.4.
Solution
time.strptime is in Python 2.4. It returns a time tuple, which can be then converted to a datetime as shown below.
start = time.strptime('2012-08-26 13:00:00', '%Y-%m-%d %H:%M:%S')
start = datetime.datetime(*start[:6])
OTHER TIPS
in python 2.4, the strptime()
function is located in the time
module.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow