0

I've installed the Chronic gem and while

I can get Chronic.parse('Saturday', :context => :past) to return last Saturday's date but not

  1. '5 last saturdays' returns 'nil'

  2. I would like it to return in this format: strftime "%m%d%Y%H%M"

  3. I also can't tag on any methods like .exists? or should ==true so I can use it to validate that those reports were run

  4. I would just like to verify that 02/11/2017 02/04/2017 01/28/2017 01/28/2017 01/21/2017 appear as text on the page, but since they will change every week i need logic that will return last 5 Saturdays from now

  • For the 3rd point, are you asking how to write the assertion in the test? It would depend on what assertion library you are using and the markup of the page. – Justin Ko Feb 14 '17 at 19:00
  • thanks that's very helpful it's rspec here's a snippet: require 'date' $time = Time.now.strftime("%m%d%Y%H%M") current_date = Date.today if arg1 == "To date" dailyops = current_date + 7 date = dailyops.strftime("%m/%d/%Y").to_s @browser.text_field(:name,"#{obj_info[1].to_s}").set "#{date}" – Johnny Lids Feb 15 '17 at 19:58
  • Sorry, I'm not sure what that snippet is trying to illustrate. Can you elaborate on where you are still having problems (eg the code you are trying and what unexpected results you are getting)? Note that you can clarify the question by editing it. Putting code in comments is very difficult to read/copy. – Justin Ko Feb 15 '17 at 22:44

1 Answers1

0

Use "weeks ago" to get the Saturday of a previous week:

Chronic.parse('1 week ago Saturday')
#=> "2017-02-11 12:00:00 -0500"
Chronic.parse('2 weeks ago Saturday')
#=> "2017-02-04 12:00:00 -0500"
Chronic.parse('3 weeks ago Saturday')
#=> "2017-01-28 12:00:00 -0500"
Chronic.parse('4 weeks ago Saturday')
#=> "2017-01-21 12:00:00 -0500"
Chronic.parse('5 weeks ago Saturday')
#=> "2017-01-14 12:00:00 -0500"

You can format the value returned using #strftime:

Chronic.parse('1 week ago Saturday').strftime("%m%d%Y%H%M")
#=> "021120171200"
Justin Ko
  • 46,526
  • 5
  • 91
  • 101