Forum: Ruby Increment day across months

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Doug B. (Guest)
on 2006-05-03 23:39
(Received via mailing list)
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
Doug B. (Guest)
on 2006-05-03 23:51
(Received via mailing list)
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.
William S. (Guest)
on 2006-05-03 23:52
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
Daniel H. (Guest)
on 2006-05-03 23:57
(Received via mailing list)
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
Trevor S. (Guest)
on 2006-05-04 00:10
(Received via mailing list)
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
Doug B. (Guest)
on 2006-05-05 15:43
(Received via mailing list)
Absolutely fantastic.  Thanks everyone - you've been a great help.
This topic is locked and can not be replied to.