How to know if today’s date is in a date range ?

How to know if today’s date is in a date range ?

Asked on December 24, 2018 in Ruby.
Add Comment


  • 11 Answer(s)

    Here is the solution,

    Try to use the function ===
    By using the === operator make a span and compare Time objects .

    start = Time.now.to_i
     
    range = start..(start + 2)
    inside = start + 1
    outside = start + 3 # ok, now...
     
    range === inside # true
    range === outside # false
    

    This code works well on everywhere like Rails, in Ruby 1, and in Ruby 2.

    Answered on December 24, 2018.
    Add Comment

    Have a look at the code below.

    irb(main):019:0> (Time.now .. (Time.now+1)) === Time.now
    TypeError: can't iterate from Time
        from (irb):19:in `each'
        from (irb):19:in `include?'
        from (irb):19:in `include?'
        from (irb):19:in `==='
        from (irb):19
        from /opt/ruby192/bin/irb:12:in `<main>'
    

    From these example , the === does not work on Ruby 1.9.2 .
    So try the #cover.

    irb(main):002:0> (Time.now..Time.now+4).cover?(Time.now)
    => true
    irb(main):003:0> (Time.now..Time.now+4).cover?(Time.now+10)
    => false
    
    Answered on December 24, 2018.
    Add Comment

    The below code for rail users. One can use the function called TimeWithZone#between?. It will give the best results.

    > start_time = Time.zone.parse('12pm')      => Thu, 26 Jul 2012 12:00:00 EDT -04:00
    > end_time = start_time + 1.hour            => Thu, 26 Jul 2012 13:00:00 EDT -04:00
    > inside = Time.zone.parse('12:30pm')       => Thu, 26 Jul 2012 12:30:00 EDT -04:00
    > outside = Time.zone.parse('1:30pm')       => Thu, 26 Jul 2012 13:30:00 EDT -04:00
    > inside.between?(start_time, end_time)     => true
    > outside.between?(start_time, end_time)    => false
    
    Answered on December 24, 2018.
    Add Comment
    irb(main):019:0> (Time.now .. (Time.now+1)) === Time.now
    TypeError: can't iterate from Time
        from (irb):19:in `each'
        from (irb):19:in `include?'
        from (irb):19:in `include?'
        from (irb):19:in `==='
        from (irb):19
        from /opt/ruby192/bin/irb:12:in `<main>'

     

    Answered on May 20, 2019.
    Add Comment
    irb(main):002:0> (Time.now..Time.now+4).cover?(Time.now)
    => true
    irb(main):003:0> (Time.now..Time.now+4).cover?(Time.now+10)
    => false
    Answered on May 20, 2019.
    Add Comment
    require 'date'
    
    today           = Date.today
    tomorrow        = today + 1
    one_month_later = today >> 1
    
    tomorrow.between?(today, one_month_later) # => true
    Answered on May 20, 2019.
    Add Comment
    def in_progress?
      (start_time..end_time).include?(Time.now)
    end
    Answered on May 20, 2019.
    Add Comment
    d1      = DateTime.parse('2018/04/01')
      d2      = DateTime.parse('2018/04/29')
      outside = DateTime.parse('2018/04/30')
      inside  = DateTime.parse('2018/04/15')
    
      # include?
      (d1...d2).include?(d1)      # true
      (d1...d2).include?(d2)      # false
      (d1...d2).include?(outside) # false
      (d1...d2).include?(inside)  # true
    
      (d1..d2).include?(d1)      # true
      (d1..d2).include?(d2)      # true
      (d1..d2).include?(outside) # false
      (d1..d2).include?(inside)  # true
    
      # ===
      (d1...d2) === d1      # true
      (d1...d2) === d2      # false
      (d1...d2) === outside # false
      (d1...d2) === inside  # true
    
      (d1..d2) === d1      # true
      (d1..d2) === d2      # true
      (d1..d2) === outside # false
      (d1..d2) === inside  # true
    
      # cover?
      (d1...d2).cover?(d1)      # true
      (d1...d2).cover?(d2)      # false
      (d1...d2).cover?(outside) # false
      (d1...d2).cover?(inside)  # true
    
      (d1..d2).cover?(d1)      # true
      (d1..d2).cover?(d2)      # true
      (d1..d2).cover?(outside) # false
      (d1..d2).cover?(inside)  # true
    
      # between?
      d1.between?(d1, d2)       # true
      d2.between?(d1, d2)       # true
      outside.between?(d1, d2)  # false
      inside.between?(d1, d2)   # true
    Answered on May 20, 2019.
    Add Comment

    please do as follows: 1. In a blank cell, says Cell B2, enter formula=IF(AND(A2>$B$1,A2<$c$1),a2, FALSE) into the Formula Bar, and press the Enter key. Note: This formula will check whether the date falls between 7/1/2008 and 12/31/2009.

    Answered on May 20, 2019.
    Add Comment

    Date.daysBetween = function( date1, date2 ) { //Get 1 day in milliseconds var one_day=1000*60*60*24; // Convert both dates to milliseconds var date1_ms = date1.getTime(); var date2_ms = date2.getTime(); // Calculate the difference in milliseconds var difference_ms = date2_ms – date1_ms; // Convert back to days and …

    Answered on May 20, 2019.
    Add Comment


  • Your Answer

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