[英]Python arrow get list of years between date range
I have this function:我有这个 function:
def function(start_date_arrow=None,end_date_arrow=None, date_concept=None):
list=[getattr(date, date_concept) for date in arrow.Arrow.range(date_concept, start_date_arrow, end_date_arrow)]
This function works well when iterating over date_concept='month'
and date_concept='day'
.这个 function 在迭代date_concept='month'
和date_concept='day'
时效果很好。 On the other hand, date_concept='year'
only returns a list of one item.另一方面, date_concept='year'
只返回一个项目的列表。
For example:例如:
start_date_arrow= arrow.get('2021-11-05')
end_date_arrow= arrow.get('2022-02-05')
year_list=function(start_date_arrow=start_date_arrow,end_date_arrow=end_date_arrow, date_concept='year')
year_list is [2021]
month_list=function(start_date_arrow=start_date_arrow,end_date_arrow=end_date_arrow, date_concept='month')
month_list is [11, 12, 1, 2]
day_list=function(start_date_arrow=start_date_arrow,end_date_arrow=end_date_arrow, date_concept='day')
day_list is [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
Second and third call are okei, but first one should return [2021,2022] instead of [2021].第二次和第三次调用是 okei,但第一次应该返回 [2021,2022] 而不是 [2021]。
Any idea of what is happening in the year call?知道年会中发生了什么吗?
Found the issue.发现了问题。 If you use:如果您使用:
start_date_arrow= arrow.get('2021-11-05')
end_date_arrow= arrow.get('2022-02-05')
Year difference between both is less than 1, so it only returns the first one, so to return 2022 in the list end_date_arrow
should be end_date_arrow= arrow.get('2022-11-05')
两者之间的年份差异小于 1,因此它只返回第一个,因此要在列表end_date_arrow
中返回 2022 应该是end_date_arrow= arrow.get('2022-11-05')
So I forced with an if
statement the end date to be bigger just by one year, to force the return of both years.所以我用if
语句强制结束日期只增加一年,以强制返回这两年。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.