0
from datetime import datetime, date, time
import time

fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.now
time.sleep(5)
d2 = datetime.now
diff = (d2-d1).seconds
print(diff)  

我收到错误消息

diff = (d2-d1).seconds
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'builtin_function_or_method'

如何以秒格式区分两个日期时间?

4

3 回答 3

0

您在()之后缺少括号now

from datetime import datetime, date, time
import time

fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.now()
time.sleep(5)
d2 = datetime.now()
diff = (d2-d1).seconds
print(diff) 
于 2013-02-07T09:59:39.490 回答
0

now是一个函数,所以你要减去的是两个函数。试试这个: datetime.now() - datetime.now()

于 2013-02-07T09:59:39.920 回答
0

问题是您没有调用 datetime.now 函数,您只是将 d1 和 d2 分配给函数的引用。这应该可以解决您的问题:

d1 = datetime.now() # note the function call
d2 = datetime.now()
于 2013-02-07T10:00:32.360 回答