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'
如何区分秒格式的两个日期时间?
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)
now
是一个函数,所以您要减去的是两个函数。试试这个:datetime.now() - datetime.now()
问题是您没有调用datetime.now函数,您只是将d1和d2分配给该函数的引用。这应该可以解决您的问题:
d1 = datetime.now() # note the function call
d2 = datetime.now()