0

I tried using answers for this question but was not able to get something that worked.

I want to be able to loop through days, starting today and going up to a target date set for an object.

The target date is of type Date.

I have tried a few things, all variations on the answers above, this was last attempt.

count = 0
Time.now..goal.target_date do 
  count += 1
end

This does not loop through anything and returns 0 as the count. Right now this is only in dev, with one object, so I know there are many days between now and the target (which is set for December 31 of this year).

I also tried this.

count = 0
Date.new(Time.now)..goal.target_date do |date|
  count += 1
end

Which returns the error undefined method 'div' for 2018-10-06 17:23:41 -0500:Time. (Same error if I use Date.today just with :DATE instead of :Time at the end).

Can anyone help me get this to run the loop for each day between now and the target date?

Thanks!

Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61

2 Answers2

2

Just wrote this one using de Date.upto() method and it worked... you just gotta make sure that 'goal.target_date' is also a valid instance of Date

require 'date'

from = Date.today
goto = from + 3

from.upto(goto) do |date|
  puts date
end
George Neto
  • 681
  • 4
  • 8
1

Do you need a loop? If not, try this:

count = (goal.target_date - Date.current).to_i

I hope it useful for you. :)

Sandra
  • 358
  • 3
  • 5