How to get 1 month in the future

Hello there all, i am writing a function that will create a list of all the
months between two given dates

def month_list ( start_y, start_m, finish_y, finish_m) # start and end month
and year
  start = Time.mktime(start_y, start_m, 1)
  end = Time.mktime(finish_y, finish_m)

  not_finished = 1
  while not_finished == 1 do

  #somehow get 1 month from start

  end

This is as far as i have gotten so far. What i mostly need to know is, how
do i get a datetime object
that is 1 month from start ?

thanks

shawn

If you use a Date object, the >> and << operators are used to add /
substract months from that date.

luis

···

On 1/8/07, shawn bright <nephish@gmail.com> wrote:

This is as far as i have gotten so far. What i mostly need to know is, how
do i get a datetime object
that is 1 month from start ?

<=>, >>, %w, define?, .., ... are actually nice in RUBY, although I felt they were boring when I firstly saw them.
def month_list(start_y,start_m,end_y,end_m)
    st = Date.civil(start_y,start_m);
    et = Date.civil(end_y,end_m);
    while((st<=>et)!=1)
        p st.to_s; st>>=1;
    end
end

Shiwei
((The views expressed are my own and not necessarily those of Oracle and its affiliates.))

shawn bright wrote:

···

Hello there all, i am writing a function that will create a list of all the
months between two given dates

def month_list ( start_y, start_m, finish_y, finish_m) # start and end month
and year
start = Time.mktime(start_y, start_m, 1)
end = Time.mktime(finish_y, finish_m)

not_finished = 1
while not_finished == 1 do

#somehow get 1 month from start

end

This is as far as i have gotten so far. What i mostly need to know is, how
do i get a datetime object
that is 1 month from start ?

thanks

shawn

shawn bright wrote:

Hello there all, i am writing a function that will create a list of all
the
months between two given dates

require 'rubygems'
require_gem 'actionpack'
1.month.from_now

got to love actionpack.. :slight_smile:

ilan

···

--
Posted via http://www.ruby-forum.com/\.

You just have to be aware that all that does is add 30 days to today's
date. This is not neccessarily the same as getting 1 month from today,
which is actually a bit more complex that one would think at first glance.

···

On 1/9/07, Ilan Berci <coder68@yahoo.com> wrote:

shawn bright wrote:
> Hello there all, i am writing a function that will create a list of all
> the
> months between two given dates
>

require 'rubygems'
require_gem 'actionpack'
1.month.from_now

got to love actionpack.. :slight_smile:

ilan

--
===Tanner Burson===
tanner.burson@gmail.com
http://www.tannerburson.com

> require 'rubygems'
> require_gem 'actionpack'
> 1.month.from_now
>
> got to love actionpack.. :slight_smile:

You just have to be aware that all that does is add 30 days to today's
date. This is not neccessarily the same as getting 1 month from today,
which is actually a bit more complex that one would think at first glance.

Yow. That's definitely worth knowing.

Anyway, for the original poster, if you go here:

http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/

And click on ">>" or "<<" you will find Luis' suggestion totally
works. These methods step backwards or forwards by month with Date
objects. (I have to admit, I'm kind of cuious why ActionPack doesn't
just use this to get a 1.month.from_now which really is one month from
now.)

···

--
Giles Bowkett
http://www.gilesgoatboy.org

http://gilesgoatboy.blogspot.com

> require 'rubygems'
> require_gem 'actionpack'
> 1.month.from_now
>
> got to love actionpack.. :slight_smile:
>
> ilan

You just have to be aware that all that does is add 30 days to today's
date.

The correct way to go about with active_support/action_pack/rails it is:

require 'rubygems'
require 'active_support'

time = Time.now.months_since(1)
or
time = Time.now.months.ago(-1)

Note that active_support is the library that you're after, not
actionpack (which is the library that provides the controller/view
behaviour of rails) - it just happened to work in the previous example
because actionpack requires active_support.

Dan.

Another option is to use chronic

require 'chronic'
puts Chronic.parse("1 month from now")

Giles Bowkett wrote:

···

> > require 'rubygems'
> > require_gem 'actionpack'
> > 1.month.from_now
> >
> > got to love actionpack.. :slight_smile:
>
> You just have to be aware that all that does is add 30 days to today's
> date. This is not neccessarily the same as getting 1 month from today,
> which is actually a bit more complex that one would think at first glance.

Yow. That's definitely worth knowing.

Anyway, for the original poster, if you go here:

http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/

And click on ">>" or "<<" you will find Luis' suggestion totally
works. These methods step backwards or forwards by month with Date
objects. (I have to admit, I'm kind of cuious why ActionPack doesn't
just use this to get a 1.month.from_now which really is one month from
now.)

--
Giles Bowkett
http://www.gilesgoatboy.org
http://gilesbowkett.blogspot.com
http://gilesgoatboy.blogspot.com