Calculating business day

I want calculate the number of business day for the current month, but I
don’t know how I skip the holidays and weekends.

Could anyone help me with this?

Thanks

Holidays you need to have defined somewhere, as they probably vary
depending on the business you are dealing with at the time (Government
versus private industry, etc).

A brute force method would be to get the dates for the start of the
month, the end of the month and step through the month from start to end
by day.

Check the day of the week for each date. Saturday or Sunday and it’s
out. Match one of your holidays, and its out.

What’s left are your business days.

Ar Chron wrote:

A brute force method would be to get the dates for the start of the
month, the end of the month and step through the month from start to end
by day.

Check the day of the week for each date. Saturday or Sunday and it’s
out. Match one of your holidays, and its out.

What’s left are your business days.

Or create a set of all dates for the year, a set of all weekend dates
for the year, and a set of all holidays for the year. Then intersect the
three sets and you end up with business days.

Now store that set in the database (after all there are only 365/366
days/year so it’s not going to be a load on the database). Now whenever
you need to iterate though business days just grab the set from the
database:

Example:

Assume named_scope “days_in_month” on BusinessDays

business_days = BusinessDays.days_in_month(“2”)
business_days.each do |day|
puts day.to_s
end

Of course don’t forget to regenerate your stored business days if
holidays change.

For the holidays you are going to need to store them somewhere as was
already pointed out in another post since they might even vary by
company. Calculating the weekend days should be easy using the
extended functionality for dates that Rails offers. You can check the
ActiveSupport::CoreExtensions::DateTime::* and
ActiveSupport::CoreExtensions::Date* modules for that.

Good luck.

On Sep 18, 2:17 pm, Penelope W. [email protected]

Sorry, I should also have added to take a look at
ActiveSupport::CoreExtensions::Numeric::Time, which allows something
like 1.week.from_now, 1.week.ago, etc.

Thanks for all help.

Finally, I could take out all the holidays. It is hard for holidays that
change every year.

2009/9/20 pepe [email protected]

We use a table of holidays here…

So take your two dates…

Decide how many days are between them
(You can just subtract the two date objects)

Subtract how many weekend days are present between the two dates…

Subtract select count(*) from holidays where date <= begin and date =>
end

Theres not much else out there thats pre-built…

Look guys, I found a way to discover the holidays.

Visit the site: code.dunae.ca/holidays

have a gem holidays, this helped me a lot, and I just want share this
information that I founded.

2009/9/29 Stephen [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs