# Calculating Dates

**URL:** <https://www.ruby-forum.com/t/calculating-dates/56515>\
**Category:** Rails\
**Created:** [March 31, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/calculating-dates/56515 "2006-03-31T20:45:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bionicwillow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@bionicwillow](https://www.ruby-forum.com/u/bionicwillow)\
**Post date:** [March 31, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/calculating-dates/56515/1 "2006-03-31T20:45:38Z")

</div>

Hello,

I am a complete newbie with Ruby and Rails. I am working on learning it  
by writing a project I was going to write in PHP using Rails. I am  
having a difficult time finding clear information on how I would do  
this.

I need to take a date out of a table in my database and given another  
day calculate the next 28 day anniversery of the original date. This is  
used to calculate a delivery schedule based on every 28 day deliveries

It is currently calculated in a spreadsheet using this method.

=B3+28 + (INT((B4-B3)/28)\*28)  
Where B3 is the start date(which will be pulled from a table) and B4 is  
the current date (or any date in the future).

So if B3 is April 3, 2006 and B4 is January 10, 2007 it will give me  
February 5, 2007.

Any tips on how I could accomplish this? I tried various methods but  
they started to get long and didn’t seem to be as clean as most of the  
Rails/Ruby code I’ve seen.

---

<div class="post-metadata">

**Author:** ![bionicwillow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@bionicwillow](https://www.ruby-forum.com/u/bionicwillow)\
**Post date:** [March 31, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/calculating-dates/56515/2 "2006-03-31T20:52:55Z")

</div>

this will kill you…

Time.now + 28.days

Mikkel B.

www.strongside.dk - Football Portal(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![bionicwillow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@bionicwillow](https://www.ruby-forum.com/u/bionicwillow)\
**Post date:** [March 31, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/calculating-dates/56515/3 "2006-03-31T21:03:12Z")

</div>

Mikkel B. wrote:

> this will kill you…
> 
> Time.now + 28.days
> 
> Mikkel B.
> 
> www.strongside.dk - Football Portal(DK)  
> ting.minline.dk - Buy Old Stuff!(DK)

Yes that will figure out what 28 days from now is, but it doesn’t give  
me what I asked.

That tells me the next day from now, but I want to calulate the next day  
based on an original date that could be 2 years in the past and a future  
date that could be 3 years from right now.

---

<div class="post-metadata">

**Author:** ![bionicwillow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@bionicwillow](https://www.ruby-forum.com/u/bionicwillow)\
**Post date:** [March 31, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/calculating-dates/56515/4 "2006-03-31T21:17:22Z")

</div>

On 3/31/06, Wes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> based on an original date that could be 2 years in the past and a future  
> date that could be 3 years from right now.

You have a Date object right? If so, add 28 to it.

d2 = d + 28

If it’s really a Time object, then just like Mikkel posted you add  
28.days to whatever Time object you have. (Time.now was just an  
example.)

– James

---

<div class="post-metadata">

**Author:** ![bionicwillow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@bionicwillow](https://www.ruby-forum.com/u/bionicwillow)\
**Post date:** [March 31, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/calculating-dates/56515/5 "2006-03-31T21:59:34Z")

</div>

I’m answering my own post.

This is what I wanted:

B3 = Date.new(2006, 4, 3) // Start date pulled from table  
B4 = Date.new(2006, 10, 6) // Any random date entered by user

diff = B3 + ((B4 - B3) / 28).ceil \* 28

diff.asctime // Produces “Mon Oct 16 00:00:00 2006”

---

<div class="post-metadata">

**Author:** ![bionicwillow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@bionicwillow](https://www.ruby-forum.com/u/bionicwillow)\
**Post date:** [May 3, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/calculating-dates/56515/6 "2006-05-03T18:56:31Z")

</div>

Does rails has any functions to calcualte the day(monday, tuesday) from  
the given date
