在Ruby中计算日期

时间:2018-04-30 12:16:26

标签: ruby date

我有一些特定的功能来测试日期。我遇到了绊脚石。我需要从今天开始向后循环14天以生成日期,但必须忽略周末。

示例:

今天是星期一,回到1天会把我带到星期五。回去2天会把我带到星期四。我们总是落后两天。所以,当我在星期二降落时,需要去周五和周一到周四。我让它工作了一周,但接下来的7天又开始了同样的周期。

这是我到目前为止所做的:

def date_calc_minus(number)


date = Date.today - number.to_i
  day = date.strftime('%a %e %B %Y').split(' ')[0]


  case
    when day.include?('Sun')
      puts day
      calculated_date = date - 2
    when day.include?('Sat')
      puts day
      calculated_date = date - 2
    else
      puts day
      calculated_date = date - 2
      if calculated_date.strftime('%a %e %B %Y').split(' ')[0].include?('Sun')
        calculated_date = calculated_date - 2
      elsif calculated_date.strftime('%a %e %B %Y').split(' ')[0].include?('Sat')
        calculated_date = calculated_date - 2
      end
  end

  calculated_date.strftime('%a %e %B %Y')
end

start = 1
stop = 14
range = start.to_i.upto(stop.to_i).to_a

range.each_with_index do |number, index|
  puts date_calc_minus(number)
end


#=>



Sun
Fri 27 April 2018
Sat
Thu 26 April 2018
Fri
Wed 25 April 2018
Thu
Tue 24 April 2018
Wed
Mon 23 April 2018
Tue
Fri 20 April 2018
Mon
Thu 19 April 2018
Sun
Fri 20 April 2018
Sat
Thu 19 April 2018
Fri
Wed 18 April 2018
Thu
Tue 17 April 2018
Wed
Mon 16 April 2018
Tue
Fri 13 April 2018
Mon
Thu 12 April 2018

我明白这不是很漂亮但是我已经看了太长时间而且我已成为隧道视野。有没有一种简单的方法来实现这一目标?

2 个答案:

答案 0 :(得分:5)

请注意使用sunday?方法确定某个日期是否恰好是星期日。

require "date"
def date_calc_minus(number)
  d = Date.today
  number.times do
    d -= 1
    d -= 2 if d.sunday?
  end
  d
end

答案 1 :(得分:1)

我的解决方案强调效率而牺牲简单性。我假设开始日期是工作日。

require 'date'

def lookin_back(start_date, n)
  days_after_monday = start_date.wday - 1
  return start_date - n if n <= days_after_monday
  weeks, days = (n - days_after_monday).divmod(5)
  start_date - days_after_monday - 7*weeks - (days > 0 ? days + 2 : 0) 
end

start_date = Date.today
  #=> #<Date: 2018-04-30 ((2458239j,0s,0n),+0s,2299161j)>
start_date.wday
  #=> 1 (Monday)

lookin_back(start_date, 0)
  #=> #<Date: 2018-04-30 ((2458239j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 1)
  #=> #<Date: 2018-04-27 ((2458236j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 5)
  #=> #<Date: 2018-04-23 ((2458232j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 6)
  #=> #<Date: 2018-04-20 ((2458229j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 7)
  #=> #<Date: 2018-04-19 ((2458228j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 11)
  #=> #<Date: 2018-04-13 ((2458222j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 16)
  #=> #<Date: 2018-04-06 ((2458215j,0s,0n),+0s,2299161j)>

start_date += 2
  #=> #<Date: 2018-05-02 ((2458241j,0s,0n),+0s,2299161j)>
start_date.wday
  #=> 3 (Wednesday)

lookin_back(start_date, 0)
  #=> #<Date: 2018-05-02 ((2458241j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 1)
  #=> #<Date: 2018-05-01 ((2458240j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 2)
  #=> #<Date: 2018-04-30 ((2458239j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 3)
  #=> #<Date: 2018-04-27 ((2458236j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 5)
  #=> #<Date: 2018-04-25 ((2458234j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 10)
  #=> #<Date: 2018-04-18 ((2458227j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 15)
  #=> #<Date: 2018-04-11 ((2458220j,0s,0n),+0s,2299161j)>
lookin_back(start_date, 12345)
  #=> #<Date: 1971-01-06 ((2440958j,0s,0n),+0s,2299161j)>