通过传递这些参数我想获得month
和(1 to 12)
,year
值2011 to 2012
和starting day
值,如ending day
特写month
和year
。我怎么能用红宝石做到这一点?
答案 0 :(得分:6)
Date.civil
听起来像你需要的那样
start_date = Date.civil(2011, 1, 1) #=> Sat, 01 Jan 2011
end_date = Date.civil(2011, 12, -1) #=> Sat, 31 Dec 2011
文档:here
答案 1 :(得分:6)
除了jfornoff的出色答案外,Rails' Active Support gem附带了丰富的date and time methods:
require 'active_support/core_ext/date' #to cherry pick just date helpers
d = Date.new(2011, 4) #=> Fri, 01 Apr 2011
d.beginning_of_month #=> Fri, 01 Apr 2011
d.end_of_month #=> Sat, 30 Apr 2011
d.all_month #=> Fri, 01 Apr 2011..Sat, 30 Apr 2011
答案 2 :(得分:0)
require 'date'
start_date = Date.new(2016, 2)
#=> #<Date: 2016-02-01 ((2457420j,0s,0n),+0s,2299161j)>
end_date = (start_date >> 1) - 1
#=> #<Date: 2016-02-29 ((2457448j,0s,0n),+0s,2299161j)>