1

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

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."]

据我所知,它是那种格式,有什么想法吗?

干杯,本

4

1 回答 1

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)]    
于 2012-09-14T04:11:47.023 回答