From: Todd Benson Date: 2012-10-06T06:41:27+09:00 Subject: Re: Ruby range - verify if date lies between two specified dates On Fri, Oct 5, 2012 at 4:01 PM, ideal one wrote: > Hi All, > I came across some issue in verifying if a date lies between two > specified dates. > > now = Date.today > today = now.strftime('%d-%b-%Y') > last_Seven_Days = (now - 7).strftime('%d-%b-%Y') > > As per above, i am calculating today's date and previous seven days and > putting in format (ex: 05-Oct-2012) > > Next I am getting a date from my screen Webpage(getText) and it's stored > as a string 'initial_date' > > ui_date = '2012-10-01' > initial_date = Date.parse ui_date > initial_date = initial_date.strftime('%d-%b-%Y') > puts initial_date => result in 01-Oct-2012 > > range = (today..last_Seven_Days) > I wanted of find if '01-Oct-2012' falls between the range. > range === last_date => sometimes comes as TRUE sometimes as FALSE > > I dunno whether I am properly comparing dates or Strings..!! > any better way and stable way to do this. > > -- > Posted via http://www.ruby-forum.com/. > first_date = Date.new(2012, 10, 1) last_date = Date.new(2012, 10, 31) range = (first_date..last_date) range.include?(Date.new(2012, 10, 5)) => true range.include?(Date.new(2012, 11, 1)) => false Todd