# Increment day across months

**URL:** <https://www.ruby-forum.com/t/increment-day-across-months/59528>\
**Category:** Ruby\
**Created:** [May 3, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/increment-day-across-months/59528 "2006-05-03T21:39:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [May 3, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/increment-day-across-months/59528/1 "2006-05-03T21:39:12Z")

</div>

Hi All

I’ve spent some time trying to find what I thought would be a simple  
solution, but so far I’ve failed  
Simply put; I want to pick a start date and end date with a format of  
“01-jan-2006”. Then increment by one day the date until it reaches the  
end  
date. For example:

01-jan-2006  
02-jan-2006  
03-jan-2006  
04-jan-2006

However it also needs to clock onto the next month - I’m hoping the  
class  
can do this without any special code? So when it hits say “28-feb-2006”  
the  
next increment will be “01-mar-2006”.

This is probably incredibly simple and I’ve looked on Google and the  
Pickaxe  
book is on my lap as I type this but I’m just not seeing the answer.

Many thanks for any help.

All the best.

Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [May 3, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/increment-day-across-months/59528/2 "2006-05-03T21:51:24Z")

</div>

Always happens doesn’t it!!

As soon as you post for help you find the answer.

If you have a better way then I’d love to still hear it but this is what  
I  
found:

begin\_date = Date.new(2006, 1, 1)  
end\_date = Date.new(2006, 5, 1)

begin\_date.step(end\_date, 1) { |myDate|

# Do something with myDate

puts myDate.to\_s  
}

Gives me the incremental date (recognising month ends) all the way to  
the  
end.

Thanks everyone.

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [May 3, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/increment-day-across-months/59528/3 "2006-05-03T21:52:54Z")

</div>

Hi,

I think you will find what you need in the Date class.

Bill Stevens  
Doug B. wrote:

> Hi All
> 
> I’ve spent some time trying to find what I thought would be a simple  
> solution, but so far I’ve failed  
> Simply put; I want to pick a start date and end date with a format of  
> “01-jan-2006”. Then increment by one day the date until it reaches the  
> end  
> date. For example:
> 
> 01-jan-2006  
> 02-jan-2006  
> 03-jan-2006  
> 04-jan-2006
> 
> However it also needs to clock onto the next month - I’m hoping the  
> class  
> can do this without any special code? So when it hits say “28-feb-2006”  
> the  
> next increment will be “01-mar-2006”.
> 
> This is probably incredibly simple and I’ve looked on Google and the  
> Pickaxe  
> book is on my lap as I type this but I’m just not seeing the answer.
> 
> Many thanks for any help.
> 
> All the best.
> 
> Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [May 3, 2006, 10:10pm UTC](https://www.ruby-forum.com/t/increment-day-across-months/59528/4 "2006-05-03T22:10:18Z")

</div>

Hi Doug,

using a range might be clearer:

begin\_date = Date.parse(‘01-jan-2006’)  
end\_date = Date.parse(‘18-mar-2006’)  
(begin\_date…end\_date).each {|date| puts date.strftime(‘%d-%b-%Y’)}

## Regards, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [May 3, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/increment-day-across-months/59528/5 "2006-05-03T21:57:46Z")

</div>

On May 3, 2006, at 9:48 PM, Doug B. wrote:

> }  
> require ‘date’  
> date\_range = Date.new(2006, 1, 1)…Date.new(2006, 5, 1)  
> date\_range.each { |day| … }  
> date\_range.to\_a # =\> […dates…]

– Daniel

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [May 5, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/increment-day-across-months/59528/6 "2006-05-05T13:43:26Z")

</div>

Absolutely fantastic. Thanks everyone - you’ve been a great help.
