0

我正在尝试为一个日历创建一个日期数组,其中下个月和上个月有一些额外的日子将填满一周。

这是我当前尝试获取数组的方法

def calendar
  selected_month = Date.civil((Time.zone.now.year).to_i, (Time.zone.now.month).to_i)

  start_date = selected_month.beginning_of_month
  start_date.sunday? ? start_date : start_date.beginning_of_week.advance(:days => -1)

  end_date = selected_month.end_of_month
  end_date.sunday? ? end_date.advance(:days => 1).end_of_week : end_date

  #only puts 1-30/31 and does not include the extra off set of days from start and end. :( 
  date_range = (start_date..end_date).to_a   
end

问题是范围仅从 1 开始并持续到月底,即使开始和结束日期超过了该值。

我不喜欢这种获取阵列的方式,所以也许你有一个更好的整体想法?

4

1 回答 1

1

您忘记重新分配 start_date 和 end_date 的值。

  start_date = selected_month.beginning_of_month
  start_date = start_date.sunday? ? start_date : start_date.beginning_of_week.advance(:days => -1)

  end_date = selected_month.end_of_month
  end_date = end_date.sunday? ? end_date.advance(:days => 1).end_of_week : end_date
于 2012-06-01T00:25:30.247 回答