我想从1月1日到12月31日得到本年度星期一的清单。
在Ruby中,最好的方法是什么?
发布于 2013-12-26 13:00:24
简单地这样说,我需要得到这个范围内所有星期一的清单。
为此使用Date#monday?:
require 'date'
start_date = Date.new(2013,1,1)
end_date = Date.new(2013,12,31)
(start_date..end_date).select(&:monday?)发布于 2013-12-26 13:46:47
添加到anwer,
start_date = (start_date = Date.new(2003,1,1)).monday? ? start_date : start_date.next_week
end_date = Date.new(2013,12,31)基准测试
(start_date..end_date).select(&:monday?)
[#<Benchmark::Tms:0x007f9309191f88 @label="", @real=0.006969, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
(start_date..end_date).step(7)
[#<Benchmark::Tms:0x007f9309191da8 @label="", @real=0.00179, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]https://stackoverflow.com/questions/20785253
复制相似问题