i have two dates i.e.
我有两个约会。
start date = "2013-06-01"
end date = "2013-12-01"
I am trying to achieve how many times mondays and tuesdays occur within this date range?
我正在努力实现在这个日期范围内星期一和星期二发生了多少次?
specific_days = 2 #monday and tuesday
total days = end_date - start_date
total_weeks = total_days/7
total_specific_days = total_weeks * specific_days
but this doesnt assures the mondays and tuesdays occurences
但这并不能保证周一和周二会发生
4 个解决方案
#1
2
This question is very similar to How do I get all Sundays between two dates in Ruby?.
这个问题非常类似于我如何在Ruby中得到两个约会之间的所有星期天。
Get all days between this dates and select the days, which weekday number is 1
or 2
. Sunday is the first weekday (= 0
).
在这个日期之间找一天的时间,选择工作日的天数为1或2。星期天是第一个工作日。
require 'date'
start_date = DateTime.parse('2013-06-01')
end_date = DateTime.parse('2013-12-01')
result = (start_date..end_date).to_a.select { |d| [1,2].include?(d.wday) }
result.count
The result is 52
times.
结果是52倍。
#2
4
Use wday method of Date class that returns the day of week (0-6, Sunday is zero), so Mon is 1 and Tue is 2.
使用日期类的wday方法返回星期的日期(0-6,星期日为0),因此Mon是1,Tue是2。
require 'date'
dates = [*Date.new(2014,1,1)..Date.new(2014,10,1)]
p dates.count{|d| (d.wday == 1) || (d.wday == 2) }
Result:
结果:
#=> 78
Updated
更新
To compare array
and range
sets:
比较数组和范围集:
require 'date'
require 'benchmark'
d1 = [*Date.new(2013,2,11)..Date.new(2013,12,25)]
d2 = Date.new(2013,2,11)..Date.new(2013,12,25)
n = 10000
Benchmark.bm do |x|
x.report('array') { n.times do d1.count{|d| (d.wday == 1) || (d.wday == 2) }; end }
x.report('range') { n.times do d2.count{|d| (d.wday == 1) || (d.wday == 2) }; end }
end
Result:
结果:
user system total real
array 0.405000 0.000000 0.405000 ( 0.419041)
range 1.498000 0.000000 1.498000 ( 1.505150)
#3
3
A bit more verbose using Date#monday?
, Date#tuesday?
and Enumerable#count
:
在周一的日期上有点啰嗦?周二,日期# ?和可列举的#数:
require 'date'
start_date = Date.parse("2013-06-01")
end_date = Date.parse("2013-12-01")
(start_date..end_date).count { |d| d.monday? || d.tuesday? }
#=> 78
#4
2
=> (Date.parse(start_date)..Date.parse(end_date)).count{|d| d.wday == 1 || d.wday == 2 }
# 52
#1
2
This question is very similar to How do I get all Sundays between two dates in Ruby?.
这个问题非常类似于我如何在Ruby中得到两个约会之间的所有星期天。
Get all days between this dates and select the days, which weekday number is 1
or 2
. Sunday is the first weekday (= 0
).
在这个日期之间找一天的时间,选择工作日的天数为1或2。星期天是第一个工作日。
require 'date'
start_date = DateTime.parse('2013-06-01')
end_date = DateTime.parse('2013-12-01')
result = (start_date..end_date).to_a.select { |d| [1,2].include?(d.wday) }
result.count
The result is 52
times.
结果是52倍。
#2
4
Use wday method of Date class that returns the day of week (0-6, Sunday is zero), so Mon is 1 and Tue is 2.
使用日期类的wday方法返回星期的日期(0-6,星期日为0),因此Mon是1,Tue是2。
require 'date'
dates = [*Date.new(2014,1,1)..Date.new(2014,10,1)]
p dates.count{|d| (d.wday == 1) || (d.wday == 2) }
Result:
结果:
#=> 78
Updated
更新
To compare array
and range
sets:
比较数组和范围集:
require 'date'
require 'benchmark'
d1 = [*Date.new(2013,2,11)..Date.new(2013,12,25)]
d2 = Date.new(2013,2,11)..Date.new(2013,12,25)
n = 10000
Benchmark.bm do |x|
x.report('array') { n.times do d1.count{|d| (d.wday == 1) || (d.wday == 2) }; end }
x.report('range') { n.times do d2.count{|d| (d.wday == 1) || (d.wday == 2) }; end }
end
Result:
结果:
user system total real
array 0.405000 0.000000 0.405000 ( 0.419041)
range 1.498000 0.000000 1.498000 ( 1.505150)
#3
3
A bit more verbose using Date#monday?
, Date#tuesday?
and Enumerable#count
:
在周一的日期上有点啰嗦?周二,日期# ?和可列举的#数:
require 'date'
start_date = Date.parse("2013-06-01")
end_date = Date.parse("2013-12-01")
(start_date..end_date).count { |d| d.monday? || d.tuesday? }
#=> 78
#4
2
=> (Date.parse(start_date)..Date.parse(end_date)).count{|d| d.wday == 1 || d.wday == 2 }
# 52