How do I add x months to a date

Discussion in 'Ruby' started by Ben Edwards, Jul 3, 2007.

  1. Ben Edwards

    Ben Edwards Guest

    I have a date

    my_date = Date.new( 2007, 1, 1 )

    I then have a variable holding an integer

    months_to_add = 4

    How do i add 'months_to_add' to my_date'?

    I have spent ages googeling and looking in o'reilly ruby cookbook but no luck;(

    Any ideas?

    Ben
    --
    Ben Edwards - Bristol, UK
    If you have a problem emailing me use
    http://www.gurtlush.org.uk/profiles.php?uid=4
    (email address this email is sent from may be defunct)
    Ben Edwards, Jul 3, 2007
    #1
    1. Advertising

  2. Ben Edwards

    Guest

    On Jul 3, 10:13 am, "Ben Edwards" <> wrote:
    > I have a date
    >
    > my_date = Date.new( 2007, 1, 1 )
    >
    > I then have a variable holding an integer
    >
    > months_to_add = 4
    >
    > How do i add 'months_to_add' to my_date'?
    >
    > I have spent ages googeling and looking in o'reilly ruby cookbook but no luck;(
    >
    > Any ideas?



    My first question is "which 4 months?" Adding January, February,
    March, and April is different from adding June, July, and August and
    September.

    However, let's assume that you know which four and can determine the
    number of days in your 4 months. According to ri, Date defines the "+"
    method, so it should be as simple as

    my_new_date = my_date + days_to_add
    , Jul 3, 2007
    #2
    1. Advertising

  3. Ben Edwards wrote:
    > I have a date
    >
    > my_date = Date.new( 2007, 1, 1 )
    >
    > I then have a variable holding an integer
    >
    > months_to_add = 4
    >
    > How do i add 'months_to_add' to my_date'?
    >
    > I have spent ages googeling and looking in o'reilly ruby cookbook but no
    > luck;(
    >
    > Any ideas?
    >
    > Ben


    I would do it like this, if I didn't mind loading some helpful
    ActiveSupport junk:

    irb> require 'active_support/core_ext/numeric'
    => true
    irb> require 'active_support/core_ext/date'
    => true

    irb> my_date = Date.new( 2007, 1, 1 )
    => #<Date: 4908203/2,0,2299161>

    irb> my_time = my_date.to_time
    => Mon Jan 01 00:00:00 +0000 2007

    irb> months_to_add = 4
    => 4

    irb> new_time = my_time + months_to_add.months
    => Tue May 01 01:00:00 +0100 2007

    irb> new_date = Date.new(new_time.year, new_time.month, new_time.day)
    => #<Date: 4908443/2,0,2299161>

    best,
    Dan

    --
    Posted via http://www.ruby-forum.com/.
    Daniel Lucraft, Jul 3, 2007
    #3
  4. Ben Edwards

    Ben Edwards Guest

    On 03/07/07, Glen Holcomb <> wrote:
    > And when I say string you could do better and use a hash or an array.
    >
    > On 7/3/07, Glen Holcomb <> wrote:
    > >
    > > You can't do it directly, date objects are immutable.
    > >
    > > You could always get the date value from the object and then parse it, add
    > > the number of months you want and then create a new date object. That
    > > seems
    > > like a bit of a waste though. I would keep the date value in a string and
    > > manipulate it there, if and when I needed an actual date object with that
    > > value I would create a date object and give that out.


    This all seems like a lot of messing around for what should be a
    standard operation. This type of thing must exist in one of the
    standard libraries.

    Ben

    > > On 7/3/07, Ben Edwards <> wrote:
    > > >
    > > > I have a date
    > > >
    > > > my_date = Date.new( 2007, 1, 1 )
    > > >
    > > > I then have a variable holding an integer
    > > >
    > > > months_to_add = 4
    > > >
    > > > How do i add 'months_to_add' to my_date'?
    > > >
    > > > I have spent ages googeling and looking in o'reilly ruby cookbook but no
    > > > luck;(
    > > >
    > > > Any ideas?
    > > >
    > > > Ben
    > > > --
    > > > Ben Edwards - Bristol, UK
    > > > If you have a problem emailing me use
    > > > http://www.gurtlush.org.uk/profiles.php?uid=4
    > > > (email address this email is sent from may be defunct)
    > > >
    > > >

    > >
    > >
    > > --
    > > "Hey brother christian with your high and mighty errand, Your actions
    > > speak
    > > so loud, I can't hear a word you're saying."
    > >
    > > -Greg Graffin (Bad Religion)
    > >

    >
    >
    >
    > --
    > "Hey brother christian with your high and mighty errand, Your actions speak
    > so loud, I can't hear a word you're saying."
    >
    > -Greg Graffin (Bad Religion)
    >



    --
    Ben Edwards - Bristol, UK
    If you have a problem emailing me use
    http://www.gurtlush.org.uk/profiles.php?uid=4
    (email address this email is sent from may be defunct)
    Ben Edwards, Jul 3, 2007
    #4
  5. Ben Edwards wrote:
    > On 03/07/07, Glen Holcomb <> wrote:
    >> > manipulate it there, if and when I needed an actual date object with that
    >> > value I would create a date object and give that out.

    >
    > This all seems like a lot of messing around for what should be a
    > standard operation. This type of thing must exist in one of the
    > standard libraries.
    >
    > Ben


    Actually, you are totally right. The '>>' operator does what is
    required:

    irb> my_date = Date.new( 2007, 1, 1 )
    => #<Date: 4908203/2,0,2299161>

    irb> my_date.to_s
    => "2007-01-01"
    irb> (my_date >> 4).to_s
    => "2007-05-01"

    best,
    Dan

    --
    Posted via http://www.ruby-forum.com/.
    Daniel Lucraft, Jul 3, 2007
    #5
  6. Ben Edwards

    Phrogz Guest

    On Jul 3, 8:13 am, "Ben Edwards" <> wrote:
    > I have a date
    >
    > my_date = Date.new( 2007, 1, 1 )
    >
    > I then have a variable holding an integer
    >
    > months_to_add = 4
    >
    > How do i add 'months_to_add' to my_date'?


    # http://phrogz.net/RubyLibs/rdoc/files/MutableTime_rb.html

    irb(main):001:0> require 'MutableTime'
    => true

    irb(main):002:0> my_date = MutableTime.new( 2007, 1, 1 )
    => Thu Feb 01 00:00:00 -0700 2007

    irb(main):003:0> my_date.month += 4
    => 5

    irb(main):004:0> my_date
    => Fri Jun 01 00:00:00 -0600 2007
    Phrogz, Jul 4, 2007
    #6
  7. Ben Edwards

    Raimon Fs Guest

    Daniel Lucraft wrote:
    > Ben Edwards wrote:
    >> On 03/07/07, Glen Holcomb <> wrote:
    >>> > manipulate it there, if and when I needed an actual date object with that
    >>> > value I would create a date object and give that out.

    >>
    >> This all seems like a lot of messing around for what should be a
    >> standard operation. This type of thing must exist in one of the
    >> standard libraries.
    >>
    >> Ben

    >
    > Actually, you are totally right. The '>>' operator does what is
    > required:
    >
    > irb> my_date = Date.new( 2007, 1, 1 )
    > => #<Date: 4908203/2,0,2299161>
    >
    > irb> my_date.to_s
    > => "2007-01-01"
    > irb> (my_date >> 4).to_s
    > => "2007-05-01"
    >
    > best,
    > Dan


    I couldn't find any info about this operator >> with dates.

    So, for example, if I have 2007-01-01 and I want to know the last day of
    adding three months (2007-03-31) I would do:

    d = Date.new(2007,1,1)
    ((d >> 3)-1).to_s

    it works, but we can relay on this operations ?


    regards,


    rai

    --
    Posted via http://www.ruby-forum.com/.
    Raimon Fs, Dec 7, 2007
    #7
  8. Ben Edwards wrote:
    > I have a date
    >
    > my_date = Date.new( 2007, 1, 1 )
    >
    > I then have a variable holding an integer
    >
    > months_to_add = 4
    >
    > How do i add 'months_to_add' to my_date'?
    >
    > I have spent ages googeling and looking in o'reilly ruby cookbook but no
    > luck;(
    >
    > Any ideas?
    >
    > Ben


    I have not tried this but I know of something in rails that sounds
    similar:
    http://railshelp.com/ActiveSupport::CoreExtensions::Date::Calculations#months_ago

    What if you could do something like this:

    my_date = Date.new( 2007, 1, 1 )
    months_to_add = 4
    my_date = my_date.months_ago(months_to_add * -1)

    Negative of months_ago could do it.

    Shot in the dark but GL!
    --
    Posted via http://www.ruby-forum.com/.
    Lloyd Linklater, Dec 7, 2007
    #8
  9. Ben Edwards

    Raimon Fs Guest

    Lloyd Linklater wrote:
    > Ben Edwards wrote:
    >> I have a date
    >>
    >> my_date = Date.new( 2007, 1, 1 )
    >>
    >> I then have a variable holding an integer
    >>
    >> months_to_add = 4
    >>
    >> How do i add 'months_to_add' to my_date'?
    >>
    >> I have spent ages googeling and looking in o'reilly ruby cookbook but no
    >> luck;(
    >>
    >> Any ideas?
    >>
    >> Ben

    >
    > I have not tried this but I know of something in rails that sounds
    > similar:
    > http://railshelp.com/ActiveSupport::CoreExtensions::Date::Calculations#months_ago


    I wasn't aware of this, thanks !!!

    I'm using Rails so I'll try to use it.

    I prefer to do in Ruby if it's possible ...


    > What if you could do something like this:
    >
    > my_date = Date.new( 2007, 1, 1 )
    > months_to_add = 4
    > my_date = my_date.months_ago(months_to_add * -1)
    >
    > Negative of months_ago could do it.
    >
    > Shot in the dark but GL!


    I'll try it ...

    thanks,


    rai
    --
    Posted via http://www.ruby-forum.com/.
    Raimon Fs, Dec 7, 2007
    #9
  10. On 12/7/07, Raimon Fs <> wrote:

    > I couldn't find any info about this operator >> with dates.
    >
    > So, for example, if I have 2007-01-01 and I want to know the last day of
    > adding three months (2007-03-31) I would do:
    >
    > d = Date.new(2007,1,1)
    > ((d >> 3)-1).to_s
    >
    > it works, but we can relay on this operations ?


    It's standard Ruby

    shadowfax:~/ssanta rick$ qri "Date#<<"
    ---------------------------------------------------------------- Date#<<
    <<(n)
    ------------------------------------------------------------------------
    Return a new Date object that is n months earlier than the current
    one.

    If the day-of-the-month of the current Date is greater than the
    last day of the target month, the day-of-the-month of the returned
    Date will be the last day of the target month.

    shadowfax:~/ssanta rick$ qri "Date#>>"
    ---------------------------------------------------------------- Date#>>
    >>(n)

    ------------------------------------------------------------------------
    Return a new Date object that is n months later than the current
    one.

    If the day-of-the-month of the current Date is greater than the
    last day of the target month, the day-of-the-month of the returned
    Date will be the last day of the target month.

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
    Rick DeNatale, Dec 7, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,203
    Michael Borgwardt
    May 30, 2004
  2. Larry Coon

    Adding months to Date

    Larry Coon, Oct 8, 2006, in forum: Java
    Replies:
    2
    Views:
    290
    Larry Coon
    Oct 8, 2006
  3. Replies:
    28
    Views:
    2,843
    Lorenzo
    Mar 16, 2009
  4. dare ruby
    Replies:
    2
    Views:
    95
    dare ruby
    Feb 12, 2010
  5. sandy

    Add six months to a start date

    sandy, Jan 12, 2004, in forum: Javascript
    Replies:
    13
    Views:
    241
    Fabian
    Jan 13, 2004
Loading...

Share This Page