Ruby Date Subtraction (e.g. 90 days Ago)

Ruby Date Subtraction (e.g. 90 days Ago)

Asked on December 24, 2018 in Ruby.
Add Comment


  • 10 Answer(s)
    require 'date'
    now = Date.today
    ninety_days_ago = (now - 90)
    

    Execute the code in the console.

    >>require 'date'
    now = Date.today
    ninety_days_ago = (now - 90)
     
    require 'date'
    => false
    now = Date.today
    => #<Date: 2011-03-02 (4911245/2,0,2299161)>
    ninety_days_ago = (now - 90)
    => #<Date: 2010-12-02 (4911065/2,0,2299161)>
    

    To get the time , use this now = DateTime.now.

    Answered on December 24, 2018.
    Add Comment

    This solution for Ruby on rails users,

    DateTime.now - 10.days
    => Sat, 04 May 2013 12:12:07 +0300
     
    20.days.ago - 10.days
    => Sun, 14 Apr 2013 09:12:13 UTC +00:00
    
    Answered on December 24, 2018.
    Add Comment

    For Ruby on Rails users or those do not mind including ActiveSupport, They have to use the Numeric#days DSL like this example:

    ruby-1.9.2-p136 :002 > Date.today
    => Wed, 02 Mar 2011
    ruby-1.9.2-p136 :003 > Date.today - 90.days
    => Thu, 02 Dec 2010
    

    Instead of times ,in case one can working with dates, one should also either start with Date instances, or convert the DateTime intances with #to_date. When adding or subtracting numbers from date instances, the numbers are indirect days.

    ruby-1.9.2-p136 :016 > DateTime.now.to_date
    => #<Date: 2011-03-02 (4911245/2,0,2299161)>
    ruby-1.9.2-p136 :017 > DateTime.now.to_date - 90
    => #<Date: 2010-12-02 (4911065/2,0,2299161)>
    
    Answered on December 24, 2018.
    Add Comment
    DateTime now = new DateTime();
    DateTime ninetyDaysAgo = now.minusDays(90);
    Answered on May 20, 2019.
    Add Comment
    >>require 'date'
    now = Date.today
    ninety_days_ago = (now - 90)
    
    require 'date'
    => false
    now = Date.today
    => #<Date: 2011-03-02 (4911245/2,0,2299161)>
    ninety_days_ago = (now - 90)
    => #<Date: 2010-12-02 (4911065/2,0,2299161)>
    Answered on May 20, 2019.
    Add Comment
    ruby-1.9.2-p136 :016 > DateTime.now.to_date
     => #<Date: 2011-03-02 (4911245/2,0,2299161)> 
    ruby-1.9.2-p136 :017 > DateTime.now.to_date - 90
     => #<Date: 2010-12-02 (4911065/2,0,2299161)> 
    Answered on May 20, 2019.
    Add Comment
     irb
     > require 'date'
     => true 
     > (DateTime.new(2015,4,1) - 90).to_s  # Apr 1, 2015 - 90 days
     => "2015-01-01T00:00:00+00:00"
     > (DateTime.new(2015,4,1) - 1).to_s   # Apr 1, 2015 - 1 day
     => "2015-03-31T00:00:00+00:00" 

    Use the #<< and #>> methods to operate on mon

    Answered on May 20, 2019.
    Add Comment
    now =  DateTime.now
    ninety_days_ago = now - 90

     

    Answered on May 20, 2019.
    Add Comment
    1. Time.now
    2. # 2018-10-19 15:43:20 +0200
    3. Time.new(2018, 1, 1)
    4. # 2018-01-01 00:00:00 +0100
    5. Time.at(15000000000)
    Answered on May 20, 2019.
    Add Comment
    1. t = Time.now
    2. puts t.day
    3. puts t.month
    4. puts t.hour
    Answered on May 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.