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
on 2007-01-08 17:37
on 2007-01-08 19:28
On 1/8/07, shawn bright <firstname.lastname@example.org> 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
on 2007-01-09 11:58
<=>, >>, %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.))
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
on 2007-01-09 17:22
On 1/9/07, Ilan B. <email@example.com> 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.
on 2007-01-09 18:47
> > 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.)
on 2007-01-19 17:29
Another option is to use chronic require 'chronic' puts Chronic.parse("1 month from now")
on 2007-01-19 17:29
> 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.