# Recurring Billing

**URL:** <https://www.ruby-forum.com/t/recurring-billing/122691>\
**Category:** Rails\
**Created:** [December 10, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/recurring-billing/122691 "2007-12-10T20:18:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [December 10, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/recurring-billing/122691/1 "2007-12-10T20:18:11Z")

</div>

Hi all,

I’m working on an invoicing program and need a bit of help working  
with dates. Given a start date, end date, and frequency (i.e.,  
weekly, monthly, quarterly), I need to be able to figure out the  
“window” that a certain date fits in.

For example, if charges are monthly and start on 1/1/07, I need a way  
to figure out that 3/4/2007 is in the window of 3/1/2007 through  
3/31/2007.

Or if charges are weekly and start on 1/1/07, I need a way to figure  
out that 1/10/2007 is in the window of 1/7/2007 through 1/13/2007.

I’m sure Ruby/Rails has a clever way to handle this, and would really  
appreciate any help.

Thanks!

-Neal

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [December 11, 2007, 7:14am UTC](https://www.ruby-forum.com/t/recurring-billing/122691/2 "2007-12-11T07:14:25Z")

</div>

You probably need something line Ruby Temporal Expressions (runt)

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

It does precisely this thing…

Cheers,  
Gary.

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [December 11, 2007, 10:45am UTC](https://www.ruby-forum.com/t/recurring-billing/122691/3 "2007-12-11T10:45:55Z")

</div>

On 12/10/07, Neal L [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 3/31/2007.
> 
> Or if charges are weekly and start on 1/1/07, I need a way to figure  
> out that 1/10/2007 is in the window of 1/7/2007 through 1/13/2007.
> 
> I’m sure Ruby/Rails has a clever way to handle this, and would really  
> appreciate any help.

Well you need some date objects.

start\_date = Date.parse(“1/7/2007”)  
end\_date = Date.parse(“1/13/2007”)  
date = Date.parse(“1/10/2007”)

These are just examples of how to get date objects.

Now you can do a range test:

if (start\_date…end\_date).include?(date)

using three dots instead of two to specify the range gives a range  
which excludes the last value so another way to do this could be:

if (start\_date…start\_date + 7).include?(date)

## HTH

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [December 11, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/recurring-billing/122691/4 "2007-12-11T13:14:42Z")

</div>

On Dec 10, 12:49 pm, “Rick DeNatale” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > weekly, monthly, quarterly), I need to be able to figure out the  
> > appreciate any help.
> 
> My blog on Rubyhttp://talklikeaduck.denhaven2.com

Rick,

Thanks for the help! The underlying question though is: how do I  
figure out the start and end dates of the range to test against?

Thanks,  
Neal
