How to calculate next, previous business day in Rails?

前端 未结 12 2328
粉色の甜心
粉色の甜心 2021-02-08 08:43

How to calculate next and previous business days in Rails?

12条回答
  •  抹茶落季
    2021-02-08 09:34

    You may need to calculate business days in the future starting from a Saturday or Sunday. 1 business day after a Monday is the Tuesday, 1 business day from a Sunday should also be Tuesday - the starting weekend day should be ignored. The following achieves this:

    class Date
    
      def business_days_future(inc)
        date = skip_weekend
        inc.times do
          date = date + 1
          date = date.skip_weekend
        end
        date
      end
    
      # If date is a saturday or sunday, advance to the following monday
      def skip_weekend
        if wday == 0
          self + 1
        elsif wday == 6
          self + 2
        else
          self
        end
      end
    
    end
    

提交回复
热议问题