Forum: Ruby how to get 1 month in the future

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.
shawn bright (Guest)
on 2007-01-08 17:37
(Received via mailing list)
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
Luis (Guest)
on 2007-01-08 19:28
(Received via mailing list)
On 1/8/07, shawn bright <removed_email_address@domain.invalid> 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 ?

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

luis
Shiwei Z. (Guest)
on 2007-01-09 11:58
(Received via mailing list)
<=>, >>,  %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.))
Ilan B. (Guest)
on 2007-01-09 17:02
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.. :)

ilan
Tanner B. (Guest)
on 2007-01-09 17:22
(Received via mailing list)
On 1/9/07, Ilan B. <removed_email_address@domain.invalid> wrote:
>
> got to love actionpack.. :)
>
> ilan


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.
Giles B. (Guest)
on 2007-01-09 18:47
(Received via mailing list)
> > require 'rubygems'
> > require_gem 'actionpack'
> > 1.month.from_now
> >
> > got to love actionpack.. :)
>
> 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.)
(Guest)
on 2007-01-19 17:29
(Received via mailing list)
Another option is to use chronic

require 'chronic'
puts Chronic.parse("1 month from now")
Daniel S. (Guest)
on 2007-01-19 17:29
(Received via mailing list)
> 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.
This topic is locked and can not be replied to.