aris
October 5, 2012, 11:01pm
1
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.
On Oct 5, 2012, at 14:01 , ideal one [email protected] wrote:
I dunno whether I am properly comparing dates or Strings…!!
any better way and stable way to do this.
You’re comparing strings and you don’t want to do that:
‘01-Oct-2012’.succ
=> “01-Oct-2013”
vs:
[Date.today.to_s, Date.today.succ.to_s]
=> [“2012-10-05”, “2012-10-06”]
(ignore the fact that I used #to_s here… it’s just to make the output
readable)
On Fri, Oct 5, 2012 at 4:01 PM, ideal one [email protected] wrote:
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
On Oct 5, 2012, at 17:16 , ideal one [email protected] wrote:
irb(main):005:0> range = (today…seven)
Maybe it is the superfluous parenthesis…
ri Range
ranges have a start and an end and go forward. Your range goes
backwards. So your range starts at today and sees that it is > the date
in question and replies with false.
range = seven…today
Hi Guys,
6199j,0s,0n),+0s,2299161j)>
irb(main):006:0> first_element = ‘04-OCT-2012 06:29:05’
=> “04-OCT-2012 06:29:05”
irb(main):007:0> top_date = Date.parse first_element
=> #<Date: 2012-10-04 ((2456205j,0s,0n),+0s,2299161j)>
irb(main):008:0> range.include? top_date
=> false
Ranges can’t go backwards. Do… range = (seven…today)
Todd
Todd B. wrote in post #1078765:
On Fri, Oct 5, 2012 at 4:01 PM, ideal one [email protected] wrote:
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
Hi Guys,
I tried doing this, can anyone point out why it is not
working
irb(main):003:0> today = Date.today
=> #<Date: 2012-10-05 ((2456206j,0s,0n),+0s,2299161j)>
irb(main):004:0> seven = (today - 7)
=> #<Date: 2012-09-28 ((2456199j,0s,0n),+0s,2299161j)>
irb(main):005:0> range = (today…seven)
=> #<Date: 2012-10-05 ((2456206j,0s,0n),+0s,2299161j)>…#<Date:
2012-09-28 ((245
6199j,0s,0n),+0s,2299161j)>
irb(main):006:0> first_element = ‘04-OCT-2012 06:29:05’
=> “04-OCT-2012 06:29:05”
irb(main):007:0> top_date = Date.parse first_element
=> #<Date: 2012-10-04 ((2456205j,0s,0n),+0s,2299161j)>
irb(main):008:0> range.include? top_date
=> false
I converted String ‘04-OCT-2012 06:29:05’ into Date format & was trying
to check if that date is within the mentioned RANGE.
I was Expecting a TRUE here, cos ‘2012-10-04’ falls between the range
mentioned!!
On Sat, Oct 6, 2012 at 2:43 AM, Todd B. [email protected] wrote:
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
Ranges can’t go backwards. Do… range = (seven…today)
There is also an approach which does not need ranges:
irb(main):001:0> d = 3.times.map {sleep 1; Time.now}
=> [2012-10-06 11:56:34 +0200, 2012-10-06 11:56:35 +0200, 2012-10-06
11:56:36 +0200]
irb(main):002:0> d[1].between? d[0], d[1]
=> true
irb(main):003:0> d[1].between? d[0], d[2]
=> true
irb(main):004:0> d[0].between? d[1], d[2]
=> false
With Date
irb(main):005:0> require ‘date’
=> true
irb(main):006:0> d = 3.times.map {|i| Date.today + i}
=> [#<Date: 2012-10-06 ((2456207j,0s,0n),+0s,2299161j)>, #<Date:
2012-10-07 ((2456208j,0s,0n),+0s,2299161j)>, #<Date: 2012-10-08
((2456209j,0s,0n),+0s,2299161j)>]
irb(main):007:0> puts d
2012-10-06
2012-10-07
2012-10-08
=> nil
irb(main):008:0> d[1].between? d[0], d[2]
=> true
irb(main):009:0> d[0].between? d[1], d[1]
=> false
irb(main):010:0> d[0].between? d[0], d[1]
=> true
irb(main):011:0> d[0].between? d[1], d[2]
=> false
irb(main):012:0> d[1].between? d[0], d[1]
=> true
This is for inclusive ranges. If you want to exclude either bound
just use a date +/- 1.
Kind regards
robert