How to Calculate Next, Previous Business Day in Rails

How to calculate next, previous business day in Rails?

As far as I understand, this is what you are looking for? (tested it)

require 'date'
def next_business_day(date)
skip_weekends(date, 1)
end

def previous_business_day(date)
skip_weekends(date, -1)
end

def skip_weekends(date, inc = 1)
date += inc
while date.wday == 0 || date.wday == 6
date += inc
end
date
end

You can test it as follows:

begin
t = Date.new(2009,9,11) #Friday, today
puts "Today: #{Date::DAYNAMES[t.wday]} #{Date::MONTHNAMES[t.mon]} #{t.day}"
nextday = next_business_day(t)
puts "Next B-day: #{Date::MONTHNAMES[nextday.mon]} #{nextday.day}"
previousday = previous_business_day(nextday)
puts "back to previous: #{Date::MONTHNAMES[previousday.mon]} #{previousday.day}"
yesterday = previous_business_day(previousday)
puts "yesterday: #{Date::MONTHNAMES[yesterday.mon]} #{yesterday.day}"
end

Calculate number of business days between two days

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.

How to get a last business day of a month with `business_time` gem

You don't need a gem, really

require 'time'

def last_business_day date
day = Date.parse(date).next_month
loop do
day = day.prev_day
break unless day.saturday? or day.sunday?
end
day
end

last_business_day '2014-11-01' # => '2014-11-28'
last_business_day '2014-12-01' # => '2014-12-31'

How do I calculate the next day (e.g., Monday) after X days out (e.g., 5) for any given date (including in the future)?

Using the week days as numbers (0 for sunday ... 6 for saturday), you could use the following formula to determine the desired date:

def date_of_next_X_day_Y_days_out_from_Z_start(wday, number_of_days, start_date)
day_ahead = start_date + number_of_days.days
(day_ahead) + ((7 - day_ahead.wday + wday) % 7).days
end

Some tests:

dates = [
{ wday: 1, n_days: 5, start_date: Date.parse('May 23,2015') },
{ wday: 3, n_days: 7, start_date: Date.parse('May 8,2015') },
{ wday: 5, n_days: 3, start_date: Date.parse('June 15,2015') },
{ wday: 3, n_days: 1, start_date: Date.parse('May 13,2015') },
{ wday: 5, n_days: 1, start_date: Date.parse('May 20,2015') }
]

dates.each do |date|
puts date_of_next_X_day_Y_days_out_from_Z_start(date[:wday], date[:n_days], date[:start_date])
end

# => 2015-06-01
# => 2015-05-20
# => 2015-06-19
# => 2015-05-20
# => 2015-05-22

Passing a previous date in link

To display date and pass as link parameter you can use either ruby code or jQuery .

Ruby : To get the date :
How to calculate next, previous business day in Rails?

Put such function in your application helper . And call into you view and also pass as link parameter .

jQuery :

Error creating a '< previous' and 'next >' (date) link for jQueryUI datepicker using setdate

Ruby find next Thursday (or any day of the week)

DateTime.now.next_week.next_day(3)

As @Nils Riedemann pointed out:

Keep in mind that it will not get the next thursday, but the thursday
of the next week. eg. If tomorrow was thursday, it won't return
tomorrow, but next week's thursdays.

Here is an excerpt of some code I've written recently which handles the missing case.

require 'date'

module DateTimeMixin

def next_week
self + (7 - self.wday)
end

def next_wday (n)
n > self.wday ? self + (n - self.wday) : self.next_week.next_day(n)
end

end

# Example

ruby-1.9.3-p194 :001 > x = DateTime.now.extend(DateTimeMixin)
=> #<DateTime: 2012-10-19T15:46:57-05:00 ... >

ruby-1.9.3-p194 :002 > x.next_week
=> #<DateTime: 2012-10-21T15:46:57-05:00 ... >

ruby-1.9.3-p194 :003 > x.next_wday(4)
=> #<DateTime: 2012-10-25T15:46:57-05:00 ... >

get next/previous month from a Time object

There are no built-in methods on Time to do what you want in Ruby. I suggest you write methods to do this work in a module and extend the Time class to make their use simple in the rest of your code.

You can use DateTime, but the methods (<< and >>) are not named in a way that makes their purpose obvious to someone that hasn't used them before.

Ruby code to get the date of next Monday (or any day of the week)

require 'date'

def date_of_next(day)
date = Date.parse(day)
delta = date > Date.today ? 0 : 7
date + delta
end

Date.today
#=>#<Date: 2011-10-28 (4911725/2,0,2299161)>
date_of_next "Monday"
#=>#<Date: 2011-10-31 (4911731/2,0,2299161)>
date_of_next "Sunday"
#=>#<Date: 2011-10-30 (4911729/2,0,2299161)>


Related Topics



Leave a reply



Submit