时间模块 - strptime Django验证错误

时间:2012-09-14 03:56:48

标签: python django

我写了一个函数来返回当前的会计年度日期:

def get_fiscal_year(start_month=7):
    now = datetime.datetime.now()
    if now.month >= start_month:
        return [time.strptime(str(now.year) + '-07-01', '%Y-%m-%d'), time.strptime(str(now.year + 1) + '-06-30', '%Y-%m-%d')]
    return [time.strptime(str(now.year - 1) + '-07-01', '%Y-%m-%d'), time.strptime(str(now.year) + '-06-30', '%Y-%m-%d')]

然后我在我的代码中使用它如下:

    dates = get_fiscal_year()
    start_date = dates[0]
    end_date = dates[1]
    model = DevelopmentAssessment.objects.filter(status_id__in=[8, 7, 10], decision_date__range=[start_date, end_date])

但是它引发了错误:

[u"'time.struct_time(tm_year=2012, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=183, tm_isdst=-1)' value has an invalid date format. It must be in YYYY-MM-DD format."]

据我所知,这种格式,任何想法是怎么回事?

干杯, 本

1 个答案:

答案 0 :(得分:2)

在您的函数get_fiscal_year中,当time.struct_time需要__range()类型对象时,您将返回date个对象的字典。

我建议将您的函数更改为

返回date个对象
def get_fiscal_year(start_month=7):
now = datetime.datetime.now()
if now.month >= start_month:
    return [datetime.date(year=now.year, month=7, day=1), 
            datetime.date(year=now.year + 1, month=6, day=30)]
return [datetime.date(year=now.year-1, month=7, day=1), 
            datetime.date(year=now.year, month=6, day=30)]