我想要昨天的日期,但是我收到了这个错误:
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'datetime.timedelta'
代码:
import datetime
today = datetime.date.today
day = datetime.timedelta(days = 1)
yesterday = today - day
答案 0 :(得分:5)
您必须使用()
。这样你就不会创建对方法的引用并获得" builtin_function_or_method"而是调用它并获得datetime
对象:
import datetime
today = datetime.date.today()
day = datetime.timedelta(days = 1)
yesterday = today - day