开发者

Calculate number of business days between two days

I need to calculate the number of business days between two dates. How can I pull that off using Ruby (or Rails...if there are Rails-specific helpers).

Likewise, I'd like to be able to add business days to a given date.

So if a date 开发者_运维百科fell on a Thursday and I added 3 business days, it would return the next Tuesday.


Take a look at business_time. It can be used for both the things you're asking.

Calculating business days between two dates:

wednesday = Date.parse("October 17, 2018")
monday = Date.parse("October 22, 2018")
wednesday.business_days_until(monday) # => 3

Adding business days to a given date:

4.business_days.from_now
8.business_days.after(some_date)

Historical answer

When this question was originally asked, business_time didn't provide the business_days_until method so the method below was provided to answer the first part of the question.

This could still be useful to someone who didn't need any of the other functionality from business_time and wanted to avoid adding an additional dependency.

def business_days_between(date1, date2)
  business_days = 0
  date = date2
  while date > date1
   business_days = business_days + 1 unless date.saturday? or date.sunday?
   date = date - 1.day
  end
  business_days
end

This can also be fine tuned to handle the cases that Tipx mentions in the way that you would like.


We used to use the algorithm suggested in the mikej's answer and discovered that calculating 25,000 ranges of several years each takes 340 seconds.

Here's another algorithm with asymptotic complexity O(1). It does the same calculations in 0.41 seconds.

# Calculates the number of business days in range (start_date, end_date]
#
# @param start_date [Date]
# @param end_date [Date]
#
# @return [Fixnum]
def business_days_between(start_date, end_date)
  days_between = (end_date - start_date).to_i
  return 0 unless days_between > 0

  # Assuming we need to calculate days from 9th to 25th, 10-23 are covered
  # by whole weeks, and 24-25 are extra days.
  #
  # Su Mo Tu We Th Fr Sa    # Su Mo Tu We Th Fr Sa
  #        1  2  3  4  5    #        1  2  3  4  5
  #  6  7  8  9 10 11 12    #  6  7  8  9 ww ww ww
  # 13 14 15 16 17 18 19    # ww ww ww ww ww ww ww
  # 20 21 22 23 24 25 26    # ww ww ww ww ed ed 26
  # 27 28 29 30 31          # 27 28 29 30 31
  whole_weeks, extra_days = days_between.divmod(7)

  unless extra_days.zero?
    # Extra days start from the week day next to start_day,
    # and end on end_date's week date. The position of the
    # start date in a week can be either before (the left calendar)
    # or after (the right one) the end date.
    #
    # Su Mo Tu We Th Fr Sa    # Su Mo Tu We Th Fr Sa
    #        1  2  3  4  5    #        1  2  3  4  5
    #  6  7  8  9 10 11 12    #  6  7  8  9 10 11 12
    # ## ## ## ## 17 18 19    # 13 14 15 16 ## ## ##
    # 20 21 22 23 24 25 26    # ## 21 22 23 24 25 26
    # 27 28 29 30 31          # 27 28 29 30 31
    #
    # If some of the extra_days fall on a weekend, they need to be subtracted.
    # In the first case only corner days can be days off,
    # and in the second case there are indeed two such days.
    extra_days -= if start_date.tomorrow.wday <= end_date.wday
                    [start_date.tomorrow.sunday?, end_date.saturday?].count(true)
                  else
                    2
                  end
  end

  (whole_weeks * 5) + extra_days
end


business_time has all the functionallity you want.

From the readme:

#you can also calculate business duration between two dates

friday = Date.parse("December 24, 2010")
monday = Date.parse("December 27, 2010")
friday.business_days_until(monday) #=> 1

Adding business days to a given date:

some_date = Date.parse("August 4th, 1969")
8.business_days.after(some_date) #=> 14 Aug 1969


Here is my (non gem and non holiday) weekday count example:

first_date = Date.new(2016,1,5)
second_date = Date.new(2016,1,12)
count = 0
(first_date...second_date).each{|d| count+=1 if (1..5).include?(d.wday)}
count


Take a look at Workpattern. It alows you to specify working and resting periods and can add/subtract durations to/from a date as well as calculate the minutes between two dates.

You can set up workpatterns for different scenarios such as mon-fri working or sun-thu and you can have holidays and whole or part days.

I wrote this as away to learn Ruby. Still need to make it more Ruby-ish.


Based on @mikej's answer. But this also takes into account holidays, and returns a fraction of a day (up to the hour accurancy):

def num_days hi, lo
  num_hours = 0
  while hi > lo
    num_hours += 1 if hi.workday? and !hi.holiday?
    hi -= 1.hour
  end
  num_hours.to_f / 24
end

This uses the holidays and business_time gems.


Simple script to calculate total number of working days

require 'date'
(DateTime.parse('2016-01-01')...DateTime.parse('2017-01-01')).
inject({}) do |s,e| 
   s[e.month]||=0
   if((1..5).include?(e.wday)) 
     s[e.month]+=1
   end
   s
end

# => {1=>21, 2=>21, 3=>23, 4=>21, 5=>22, 6=>22, 7=>21, 8=>23, 9=>22, 10=>21, 11=>22, 12=>22}


There are two problems with the most popular solutions listed above:

  1. They involve loops to count every single day between each date (meaning that performance gets worse the further apart the dates are.
  2. They are unclear about whether they count from the beginning of the day or the end. If you count from the morning, there is one weekday between Friday and Saturday. If you count from the night, there are zero weekdays between Friday and Saturday.

After stewing over it, I propose this solution that addresses both problems. The below takes a reference date and an other date and calculates the number of weekdays between them (returning a negative number if other is before the reference date). The argument eod_base controls whether counting is done from end of day (eod) or start of day. It could be written more compactly but hopefully it's relatively easy to understand and it doesn't require gems or rails.

  require 'date'

  def weekdays_between(ref,otr,eod_base=true)
        dates = [ref,otr].sort
        return 0 if dates[0] == dates[1]
        full_weeks = ((dates[1]-dates[0])/7).floor  
        dates[eod_base ? 0 : 1] += (eod_base ? 1 : -1)
        part_week = Range.new(dates[0],dates[1])
                         .inject(0){|m,v|  (v.wday >=1 && v.wday <= 5) ? (m+1) : m }
        return (otr <=> ref) * (full_weeks*5 + part_week)
  end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜