简体   繁体   English

在Python中从datetime.now()中减去SQL DATETIME

[英]Subtract SQL DATETIME from datetime.now() in Python

I have a DATETIME field in SQL. 我在SQL中有一个DATETIME字段。 Its content is: 2012-08-26 13:00:00 其内容是: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: 在Python 2.7中,它很简单:

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. 但我有一个运行Python 2.4的Web服务器。 In Python 2.4 strptime is not in the datetime module. 在Python 2.4中,strptime不在datetime模块中。 I can't figure out how to accomplish the same thing in 2.4. 我无法弄清楚如何在2.4中完成同样的事情。

time.strptime is in Python 2.4. time.strptime在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])

在python 2.4中, strptime()函数位于time模块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM