# Calculating business day

**URL:** <https://discuss.rubyonrails.org/t/calculating-business-day/46171>\
**Category:** rubyonrails-talk\
**Created:** [September 18, 2009, 6:17pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171 "2009-09-18T18:17:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 18, 2009, 6:17pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171/1 "2009-09-18T18:17:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 18, 2009, 9:06pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171/2 "2009-09-18T21:06:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [September 19, 2009, 10:32pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171/3 "2009-09-19T22:32:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [September 20, 2009, 4:41pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171/4 "2009-09-20T16:41:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ludmila\_Moraes](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Ludmila\_Moraes](https://discuss.rubyonrails.org/u/Ludmila_Moraes)\
**Post date:** [September 28, 2009, 9:35pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171/5 "2009-09-28T21:35:40Z")

</div>

Thanks for all help.

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

---

<div class="post-metadata">

**Author:** ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://discuss.rubyonrails.org/u/Stephen)\
**Post date:** [September 29, 2009, 7:25pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171/6 "2009-09-29T19:25:13Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Ludmila\_Moraes](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Ludmila\_Moraes](https://discuss.rubyonrails.org/u/Ludmila_Moraes)\
**Post date:** [October 5, 2009, 5:04pm UTC](https://discuss.rubyonrails.org/t/calculating-business-day/46171/7 "2009-10-05T17:04:01Z")

</div>

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

Visit the site: [code.dunae.ca/holidays](http://code.dunae.ca/holidays)

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