用python获取日期

时间:2014-06-27 13:30:11

标签: python datetime

我想要昨天的日期,但是我收到了这个错误:

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

1 个答案:

答案 0 :(得分:5)

您必须使用()。这样你就不会创建对方法的引用并获得" builtin_function_or_method"而是调用它并获得datetime对象:

import datetime 
today = datetime.date.today()
day = datetime.timedelta(days = 1)
yesterday = today - day