How to get 1 month in the future


#1

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


#2

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


#3

<=>, >>, %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.))


#4

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


#5

On 1/9/07, Ilan B. removed_email_address@domain.invalid wrote:

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. 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.


#6

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.)


#7

Another option is to use chronic

require ‘chronic’
puts Chronic.parse(“1 month from now”)


#8

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.