Howdy all, This may be the dumbest question in the world, but how can you "add one month" to a Date object? I can''t just add 30 days, because months have varying lengths. Any help would be much appreciated... Thanks! -Neal --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
if you convert it to time you can use ''advance'', and then convert it back to a date. For example: date_object.to_time.advance(1.month).to_date http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/DateTime/Calculations.html#M000714 On Dec 12, 4:20 pm, Neal L <neal.lo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Howdy all, > > This may be the dumbest question in the world, but how can you "add > one month" to a Date object? I can''t just add 30 days, because months > have varying lengths. > > Any help would be much appreciated... > > Thanks! > > -Neal--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
oops, wrong syntax. Should be: date_object.to_time.advance(:months => 1).to_date On Dec 12, 4:25 pm, ebrad <nisguy_...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> if you convert it to time you can use ''advance'', and then convert it > back to a date. For example: > > date_object.to_time.advance(1.month).to_date > > http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/DateT... > > On Dec 12, 4:20 pm, Neal L <neal.lo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Howdy all, > > > This may be the dumbest question in the world, but how can you "add > > one month" to a Date object? I can''t just add 30 days, because months > > have varying lengths. > > > Any help would be much appreciated... > > > Thanks! > > > -Neal--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 12, 2007, at 4:20 PM, Neal L wrote:> Howdy all, > > This may be the dumbest question in the world, but how can you "add > one month" to a Date object? I can''t just add 30 days, because months > have varying lengths. > > Any help would be much appreciated... > > Thanks! > > -NealIf you have a Date object (i.e, not a Time object), then use >> and << to go forward and backward by month. If you have a Time (since you''re in Rails), you can use months_ago or months_since. 2.months_since(Time.now) Compare that to (2.months).since(Time.now) particularly in the last few days of the month. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Neal L wrote:> Howdy all, > > This may be the dumbest question in the world, but how can you "add > one month" to a Date object? I can''t just add 30 days, because months > have varying lengths. > > Any help would be much appreciated... > > Thanks! > > -NealDate.today >> 1 # for one month Date.today >> 12 # for one year -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-13 04:06 UTC
Re: Add a month to a Date object
Note: IMO the best way to do this is trough << and >>. But watch out in iterating blocks, like while, for & etc. You might end up in the wrong day if you keep doing something like date >> 1 Example: date = ''2007-12-31''.to_date while date <= ''2008-3-31''.to_date puts date date >> 1 end This is going to be your output: (the formatting of the date is going to be different, this is just an example) Dec 31, 2007 Jan 31, 2008 Feb 29, 2008 Mar 29, 2008 (note that your date has day = 29) If instead you do something like: date = ''2007-12-31''.to_date c = 0 while date <= ''2008-3-31''.to_date puts date c = c + 1 #this is the same as c += 1 date = date >> c #this is the same as date =>> c end (This code could be shorter, but would be harder to understand) This is going to be your output: Dec 31, 2007 Jan 31, 2008 Feb 29, 2008 Mar 31, 2008 The difference is that in the first approach, you always do date >> 1, several times. In the second, you do date >> 1 the first time, date >> 2 the second, and so on. On Dec 12, 1:27 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Dec 12, 2007, at 4:20 PM, Neal L wrote: > > > Howdy all, > > > This may be the dumbest question in the world, but how can you "add > > one month" to a Date object? I can''t just add 30 days, because months > > have varying lengths. > > > Any help would be much appreciated... > > > Thanks! > > > -Neal > > If you have a Date object (i.e, not a Time object), then use >> and << > to go forward and backward by month. If you have a Time (since you''re > in Rails), you can use months_ago or months_since. > > 2.months_since(Time.now) > > Compare that to (2.months).since(Time.now) particularly in the last > few days of the month. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-13 05:02 UTC
Re: Add a month to a Date object
I had a brain fart. The second code is WRONG and wont work as expected. This should work. date = ''2007-12-31''.to_date c = 0 while date >> c <= ''2008-3-31''.to_date puts date >> c c = c + 1 #this is the same as c += 1 end This is going to be your output: Dec 31, 2007 Jan 31, 2008 Feb 29, 2008 Mar 31, 2008 On Dec 12, 8:06 pm, "felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Note: > > IMO the best way to do this is trough << and >>. But watch out in > iterating blocks, like while, for & etc. You might end up in the wrong > day if you keep doing something like date >> 1 > > Example: > > date = ''2007-12-31''.to_date > while date <= ''2008-3-31''.to_date > puts date > date >> 1 > end > > This is going to be your output: > (the formatting of the date is going to be different, this is just an > example) > > Dec 31, 2007 > Jan 31, 2008 > Feb 29, 2008 > Mar 29, 2008 (note that your date has day = 29) > > If instead you do something like: > > date = ''2007-12-31''.to_date > c = 0 > while date <= ''2008-3-31''.to_date > puts date > c = c + 1 #this is the same as c += 1 > date = date >> c #this is the same as date =>> c > end > (This code could be shorter, but would be harder to understand) > > This is going to be your output: > > Dec 31, 2007 > Jan 31, 2008 > Feb 29, 2008 > Mar 31, 2008 > > The difference is that in the first approach, you always do date >> 1, > several times. In the second, you do date >> 1 the first time, date >> > 2 the second, and so on. > > On Dec 12, 1:27 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > wrote: > > > On Dec 12, 2007, at 4:20 PM, Neal L wrote: > > > > Howdy all, > > > > This may be the dumbest question in the world, but how can you "add > > > one month" to a Date object? I can''t just add 30 days, because months > > > have varying lengths. > > > > Any help would be much appreciated... > > > > Thanks! > > > > -Neal > > > If you have a Date object (i.e, not a Time object), then use >> and << > > to go forward and backward by month. If you have a Time (since you''re > > in Rails), you can use months_ago or months_since. > > > 2.months_since(Time.now) > > > Compare that to (2.months).since(Time.now) particularly in the last > > few days of the month. > > > -Rob > > > Rob Biedenharn http://agileconsultingllc.com > > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---