1

我有以下节日数组:

id = 1, Start_date = 2011-11-01 , End_date = 2016-12-31

[[1, 2011-11-01 , 2016-12-31],
[2, 2011-11-28 , 2016-12-31],
[3, 2011-10-01 , 2015-12-31],
[4, 2011-11-28 , 2021-12-31],
[5, 2010-11-15 , 2016-12-31],
[6, 2011-07-01 , 2017-12-31],
[7, 2012-02-01 , 2013-02-19],
[8, 2011-03-21 , 2015-04-30],
[9, 2012-03-01 , 2016-03-20]]

我需要start_date从当前日期开始对其进行排序,但在这里忽略年份(仅使用月份和日期)。

实际上,这些是节日的开始日期和结束日期。我需要先显示正在运行的节日(按:start_date 排序),然后是即将到来的节日(按:start_date 分别排序)。

例如:

festival A: start_date: 15-may  - end_date: 15-june  
festival B: start_date: 01-june - end_date: 20-june
festival C: start_date: 01-oct  - end_date: 30-oct
  • 如果今天是 6 月 14 日,那么订单应该是A B C
  • 如果今天是 6 月 16 日,那么订单应该是:B C A
  • 最后,如果今天是 2 月 1 日,那么订单应该是:A B C

预先感谢您的回复。

4

1 回答 1

3

假设start_dateend_dateDate对象:

# get the festivals from the database
festivals = Festival.all

# define the day of interest
day = Date.today

# partition the festivals into running and upcoming
# based on the day of the year (1-366)
running, upcoming = festivals.partition { |f|
  (f.start_date.yday..f.end_date.yday).include? day.yday
}

# sort each array by day of the year
running.sort_by!{ |f| f.start_date.yday }
upcoming.sort_by!{ |f| f.start_date.yday }
于 2012-07-04T10:30:00.073 回答