在 ruby 中,我怎样才能得到一年中的第 14 天,从一个日期开始前后移动。
假设今天 2015 年 6 月 16 日我需要支付 2 周的回收费用。我怎样才能根据该日期获得今年每个回收账单日的数组。
最佳答案
日期有一个 step方法:
require 'date'
d = Date.strptime("6-16-2015", '%m-%d-%Y') # strange date format
end_year = Date.new(d.year, -1, -1)
p d.step(end_year, 14).to_a
# =>[#<Date: 2015-06-16 ((2457190j,0s,0n),+0s,2299161j)>, #<Date: 2015-06-30 ((2457204j,0s,0n),+0s,2299161j)>, ...
# Going backward:
begin_year = Date.new(d.year, 1, 1)
p d.step(begin_year,-14).to_a
# =>[#<Date: 2015-06-16 ((2457190j,0s,0n),+0s,2299161j)>, #<Date: 2015-06-02 ((2457176j,0s,0n),+0s,2299161j)>,...
关于ruby - 获取一个数组,包括从给定日期开始的每 14 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875789/