如何将整数转换为日期数组?

如果用户使用days_challenge: 5committed: ["mon", "tue", "wed", "thu", "fri"]创建挑战,那么我们如何从date_started: "2016-04-20"创建日期数组date_started: "2016-04-20"直到挑战的最后一天使用名为dates_challenged的模型方法?

 create_table "challenges", force: true do |t| t.string "action" t.date "date_started" t.text "committed", default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n" t.integer "days_challenged" end 

arrays看起来像这样: ["2016-04-20", "2016-04-21", "2016-04-22", "2016-04-25", "2016-04-26"]

 class Challenge = self.days_challenged 0 else self.days_challenged - ((date_started.to_date)..Date.yesterday).count do |date| committed_wdays.include? date.wday end end end end 

为了澄清,我正在尝试创建一个模型方法,以便我可以在视图中执行@challenge.dates_challenged ,然后我可以迭代日期数组。

这就是我提出的 – 仍然存在一些问题 – 我认为如果committed是空的,它会爆炸,但它很简洁:

 def dates_challenged date = date_started-1 days_challenge.times.map { date = find_next(date) } end private def find_next(date) break if committed.include? Date::ABBR_DAYNAMES[date.wday].downcase while date = date.next date end 

获取开始日期和结束日期 – 然后您可以获得开始和结束之间的一系列日期和/或日期

 start_date = challenge.date.to_date end_date = another_challenge.date.to_date (start_date..end_date).map(&:mday) # returns array of days of the month 

有关详细信息,请参阅列出两个日期之间的天数

简单和详细:从日期字符串(Date.parse)创建一个日期对象,并创建一个空数组来保存您“提交”的日期。 如上所示,可用的星期几列表存储在一个数组中。 循环,直到我们提交存储在数组中的天数等于请求的天数,每隔一天添加一天。 添加到date_committed数组的日期将根据星期数组的有效天数(days_committed)进行检查。

 require 'date' days_challenge = 5 days_committed = ["mon", "tue", "wed", "thu", "fri"] date_started = "2016-04-20" date_committed = [] date_started_parsed = Date.parse(date_started) while (date_committed.size < days_challenge) date_dow = date_started_parsed.strftime('%a').downcase if (days_committed.include?(date_dow)) date_committed << date_started_parsed.strftime('%F') end # if date_started_parsed = date_started_parsed + 1 end # while puts date_committed.to_s 

根据一周中的有效日期返回正确的日期。

 ["2016-04-20", "2016-04-21", "2016-04-22", "2016-04-25", "2016-04-26"] 
 committed = ["mon", "tue", "wed", "thu", "fri"] days = committed.dup count = 0 dates = [] while days.present? count += 1 weekday = (date + count).strftime('%a').downcase if days.include? weekday dates << (date + count.days).strftime('%Y-%m-%d') days.delete(weekday) end end puts dates # => ["2016-04-21", "2016-04-22", "2016-04-25", "2016-04-26", "2016-04-27"] 

Previuos回答

假设您在Rails环境中:

 days_challenge = 5 date = DateTime.parse("2016-04-20") (1..days_challenge).map {|n| (date + n.days).strftime('%Y-%m-%d') } # Outputs ["2016-04-21", "2016-04-22", "2016-04-23", "2016-04-24", "2016-04-25"]