# Want only the working hours(9am-5pm) between two dates

**URL:** <https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309>\
**Category:** Ruby\
**Created:** [December 10, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309 "2008-12-10T13:18:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [December 10, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309/1 "2008-12-10T13:18:54Z")

</div>

want only the working hours(9am-5pm) between two dates, weekends should  
be omitted…

i did this code…

require ‘time’  
require ‘date’  
class Date  
def calc(date1,date2)

```
d1=Time.parse(date1).strftime('%d').to_i
m1=Time.parse(date1).strftime('%m').to_i
y1=Time.parse(date1).strftime('%Y').to_i
t1=Time.parse(date1).strftime('%H').to_i
min1=Time.parse(date1).strftime('%M').to_i

date1 = Date.new( y1, m1, d1 )

d2=Time.parse(date2).strftime('%d').to_i
m2=Time.parse(date2).strftime('%m').to_i
y2=Time.parse(date2).strftime('%Y').to_i
t2=Time.parse(date2).strftime('%H').to_i

date2 = Date.new( y2, m2, d2 )
weekdays = (date1..date2).reject { |d| [0,6].include? d.wday }
w= weekdays.length
working_hours=w*8
puts "working hours = #{working_hours}"

```

end  
end

d=Date.new  
d.calc(“November 5th, 2008, 10:30 am”,“November 7th, 2008, 6:00 pm”)

i have calculated the total working hours…  
but i need the total hours between 9am-5pm, between the given dates…  
can any one plz help??  
thanks.,

Regards,  
Srikanth J

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [December 10, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309/2 "2008-12-10T14:26:03Z")

</div>

Srikanth J. wrote:

> but i need the total hours between 9am-5pm, between the given dates…  
> can any one plz help??

Not sure what you mean. Do you mean (date2 - date1 + 1) \* 8 ?

Use to\_i on the answer if you don’t want Rational arithmetic.

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [December 10, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309/3 "2008-12-10T14:39:05Z")

</div>

Brian C. wrote:

> Srikanth J. wrote:
> 
> > but i need the total hours between 9am-5pm, between the given dates…  
> > can any one plz help??
> 
> Not sure what you mean. Do you mean (date2 - date1 + 1) \* 8 ?
> 
> Use to\_i on the answer if you don’t want Rational arithmetic.

hi brain,

i mean…

date1=“November 5th, 2008, 10:30 am”  
date2=“November 7th, 2008, 6:00 pm”

i want only the working hours between 10:30 am to 6.00 pm,in these  
dates…

date can also be,

date1=“March 5th, 2005, 5:30 am”  
date2=“November 7th, 2006, 9:00 pm”

all i need is working hours between these dates…

first day & last days time makes complications here.

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [December 10, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309/4 "2008-12-10T14:40:23Z")

</div>

Srikanth J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> want only the working hours(9am-5pm) between two dates, weekends should  
> be omitted…
> 
> i did this code…  
> but i need the total hours between 9am-5pm, between the given dates…  
> can any one plz help??

There’s 8 hours between 9am-5pm, so 8 hour/working day.  
There’s 5 working day/week.

So if you can compute the number of weeks between the Monday following  
the first date, and the Monday preceding the last date, you can easily  
compute the number of working hours.

if week-of(start) = week-of(end) then  
working-hours = working-hours/working-day \* working-days(from start  
to end)  
else  
working-hours = working-hours/working-day \* ( working-days(from start  
to end-of-week-after(start))  
+ working-days(from  
beginning-of-week-before(end) to end)  
+  
working-days/working-weeks \* working-weeks(from end-of-week-after(start)  
to  
beginning-of-week-before(end)))  
end

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [December 10, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309/5 "2008-12-10T15:57:23Z")

</div>

Srikanth J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> date1=“March 5th, 2005, 5:30 am”  
> date2=“November 7th, 2006, 9:00 pm”
> 
> all i need is working hours between these dates…
> 
> first day & last days time makes complications here.

If you have partial days, apply the same principle than in my previous  
answer, trim off the partial days, compute the trimmed off hours, and  
multiply the days by the working-hours-per-day.

if day(start)=day(end) then  
min(5pm,hour(end)) - max(9am,hour(start))  
else  
(min(5pm,hour(end)) - 9am)

- (5pm - max(9am,hour(start)))
- previous algo with (start + 1day) and (end - 1 day)  
end

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [December 10, 2008, 5:11pm UTC](https://www.ruby-forum.com/t/want-only-the-working-hours-9am-5pm-between-two-dates/155309/6 "2008-12-10T17:11:03Z")

</div>

On Dec 10, 8:32 am, Srikanth J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> date1=“March 5th, 2005, 5:30 am”  
> date2=“November 7th, 2006, 9:00 pm”
> 
> all i need is working hours between these dates…
> 
> ## first day & last days time makes complications here.
> 
> Posted viahttp://www.ruby-forum.com/.

You could try this beauty of a gem.

[http://runt.rubyforge.org/](http://runt.rubyforge.org/)

[http://runt.rubyforge.org/doc/files/doc/tutorial\_te\_rdoc.html](http://runt.rubyforge.org/doc/files/doc/tutorial_te_rdoc.html)
