# Calculating an array of dates

**URL:** <https://www.ruby-forum.com/t/calculating-an-array-of-dates/171890>\
**Category:** Ruby\
**Created:** [July 10, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/calculating-an-array-of-dates/171890 "2009-07-10T22:30:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![epasquier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2bfe46/32.png) [@epasquier](https://www.ruby-forum.com/u/epasquier)\
**Post date:** [July 10, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/calculating-an-array-of-dates/171890/1 "2009-07-10T22:30:11Z")

</div>

I have the following test script. My objective is to have an array of  
dates between this week’s Thursday and some date in the past that I get  
out of a call to a database. You can see the output below which is not  
what I wanted. I am thinking it is in the way I return data and call my  
recursive routine. What am I missing?

I am hoping for data like, which later I can loop through:

[[Thu Jul 09 12:00:00 -0700 2009],  
[Thu Jul 02 12:00:00 -0700 2009],  
[Thu Jun 25 12:00:00 -0700 2009],  
[Thu Jun 18 12:00:00 -0700 2009],  
[Thu Jun 11 13:14:44 -0700 2009] ]

Thanks for any help ya’ll can provide.  
Eric

> > > > > > > start\<\<\<\<\<\<\<\<\<\<\<  
> > > > > > > require ‘rubygems’  
> > > > > > > require ‘Chronic’  
> > > > > > > require ‘Time’

# ---------- ---------- ---------- ---------- ---------- ----------

def get\_weeks( this\_dt, max\_dt )

# ---------- ---------- ---------- ---------- ---------- ----------

# recursively calculate thursdays

# ---------- ---------- ---------- ---------- ---------- ----------

return this\_dt.to\_a if ( this\_dt.strftime( “%Y%m%b” ) ==  
max\_dt.strftime( “%Y%m%b” ) )  
puts “get\_week\_list(): 4. this\_dt: #{this\_dt.inspect}”  
puts “get\_week\_list(): 5. max\_dt: #{max\_dt.inspect}”

next\_dt = Chronic.parse( ‘Last Thursday at noon’, :now =\> this\_dt )  
puts “get\_week\_list(): 6. next\_dt: #{next\_dt.inspect}\n\n”  
return get\_weeks( next\_dt, max\_dt ).to\_a

end # get\_weeks

# ---------- ---------- ---------- ---------- ---------- ----------

def get\_week\_list( u\_id )

# ---------- ---------- ---------- ---------- ---------- ----------

# return an array of dates between now and when this user was created

# ---------- ---------- ---------- ---------- ---------- ----------

# ---------- ---------- ----------

# what is this week’s “end point”.

# Either today or calculated

# ---------- ---------- ----------

this\_week\_end = Time.now  
puts “get\_week\_list(): Today’s day: #{this\_week\_end.wday.to\_s}”  
if Time.now.wday != 4 # not thursday

```
this_week_end = Chronic.parse( 'Last Thursday at noon', :now =>

```

Time.now )

end  
puts “get\_week\_list(): 1. this\_week\_end: #{this\_week\_end.inspect}”

# ---------- ---------- ----------

# How far back should we get a list?

# How about to when this user was created?

# ---------- ---------- ----------

max\_date = Time.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ ) # from a db  
call  
puts “get\_week\_list(): 2. max\_date’s day: #{max\_date.wday.to\_s}”  
if max\_date.wday != 4

```
max_date = Chronic.parse( 'Last Thursday at noon', :now => max_date

```

)

end  
puts “get\_week\_list(): 3. max\_date: #{max\_date.inspect}\n\n”

# ---------- ---------- ----------

# recursively calc between the two

# ---------- ---------- ----------

dates = Array.new  
dates \<\< get\_weeks( this\_week\_end, max\_date )  
return dates

end # get\_week\_list

# --------------------------------------------------------------------

dts = Array.new  
dts \<\< get\_week\_list( 2 )  
puts dts.inspect  
puts dts.class

> > > > > > > end\<\<\<\<\<\<\<\<\<\<\<

returns:

C:\A\ruby\>ruby w.rb  
get\_week\_list(): Today’s day: 5  
get\_week\_list(): 1. this\_week\_end: Thu Jul 09 12:00:00 -0700 2009  
get\_week\_list(): 2. max\_date’s day: 4  
get\_week\_list(): 3. max\_date: Thu Jun 11 13:14:44 -0700 2009

get\_week\_list(): 4. this\_dt: Thu Jul 09 12:00:00 -0700 2009  
get\_week\_list(): 5. max\_dt: Thu Jun 11 13:14:44 -0700 2009  
get\_week\_list(): 6. next\_dt: Thu Jul 02 12:00:00 -0700 2009

get\_week\_list(): 4. this\_dt: Thu Jul 02 12:00:00 -0700 2009  
get\_week\_list(): 5. max\_dt: Thu Jun 11 13:14:44 -0700 2009  
get\_week\_list(): 6. next\_dt: Thu Jun 25 12:00:00 -0700 2009

[[[0, 0, 12, 25, 6, 2009, 4, 176, true, “Pacific Daylight Time”]]]  
Array

---

<div class="post-metadata">

**Author:** ![epasquier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2bfe46/32.png) [@epasquier](https://www.ruby-forum.com/u/epasquier)\
**Post date:** [July 11, 2009, 1:20am UTC](https://www.ruby-forum.com/t/calculating-an-array-of-dates/171890/2 "2009-07-11T01:20:54Z")

</div>

Eric P. wrote:

> I have the following test script. My objective is to have an array of  
> dates between this week’s Thursday and some date in the past that I get  
> out of a call to a database. You can see the output below which is not  
> what I wanted. I am thinking it is in the way I return data and call my  
> recursive routine. What am I missing?
> 
> I am hoping for data like, which later I can loop through:
> 
> [[Thu Jul 09 12:00:00 -0700 2009],  
> [Thu Jul 02 12:00:00 -0700 2009],  
> [Thu Jun 25 12:00:00 -0700 2009],  
> [Thu Jun 18 12:00:00 -0700 2009],  
> [Thu Jun 11 13:14:44 -0700 2009] ]
> 
> Thanks for any help ya’ll can provide.  
> Eric
> 
> > > > > > > > start\<\<\<\<\<\<\<\<\<\<\<  
> > > > > > > > require ‘rubygems’  
> > > > > > > > require ‘Chronic’  
> > > > > > > > require ‘Time’
> 
> # ---------- ---------- ---------- ---------- ---------- ----------
> 
> def get\_weeks( this\_dt, max\_dt )
> 
> # ---------- ---------- ---------- ---------- ---------- ----------
> 
> # recursively calculate thursdays
> 
> # ---------- ---------- ---------- ---------- ---------- ----------
> 
> return this\_dt.to\_a if ( this\_dt.strftime( “%Y%m%b” ) ==  
> max\_dt.strftime( “%Y%m%b” ) )  
> puts “get\_week\_list(): 4. this\_dt: #{this\_dt.inspect}”  
> puts “get\_week\_list(): 5. max\_dt: #{max\_dt.inspect}”
> 
> next\_dt = Chronic.parse( ‘Last Thursday at noon’, :now =\> this\_dt )  
> puts “get\_week\_list(): 6. next\_dt: #{next\_dt.inspect}\n\n”  
> return get\_weeks( next\_dt, max\_dt ).to\_a
> 
> end # get\_weeks
> 
> # ---------- ---------- ---------- ---------- ---------- ----------
> 
> def get\_week\_list( u\_id )
> 
> # ---------- ---------- ---------- ---------- ---------- ----------
> 
> # return an array of dates between now and when this user was created
> 
> # ---------- ---------- ---------- ---------- ---------- ----------
> 
> # ---------- ---------- ----------
> 
> # what is this week’s “end point”.
> 
> # Either today or calculated
> 
> # ---------- ---------- ----------
> 
> this\_week\_end = Time.now  
> puts “get\_week\_list(): Today’s day: #{this\_week\_end.wday.to\_s}”  
> if Time.now.wday != 4 # not thursday
> 
> ```
> this_week_end = Chronic.parse( 'Last Thursday at noon', :now =>
> 
> ```
> 
> Time.now )
> 
> end  
> puts “get\_week\_list(): 1. this\_week\_end: #{this\_week\_end.inspect}”
> 
> # ---------- ---------- ----------
> 
> # How far back should we get a list?
> 
> # How about to when this user was created?
> 
> # ---------- ---------- ----------
> 
> max\_date = Time.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ ) # from a db  
> call  
> puts “get\_week\_list(): 2. max\_date’s day: #{max\_date.wday.to\_s}”  
> if max\_date.wday != 4
> 
> ```
> max_date = Chronic.parse( 'Last Thursday at noon', :now => max_date
> 
> ```
> 
> )
> 
> end  
> puts “get\_week\_list(): 3. max\_date: #{max\_date.inspect}\n\n”
> 
> # ---------- ---------- ----------
> 
> # recursively calc between the two
> 
> # ---------- ---------- ----------
> 
> dates = Array.new  
> dates \<\< get\_weeks( this\_week\_end, max\_date )  
> return dates
> 
> end # get\_week\_list
> 
> # --------------------------------------------------------------------
> 
> dts = Array.new  
> dts \<\< get\_week\_list( 2 )  
> puts dts.inspect  
> puts dts.class
> 
> > > > > > > > end\<\<\<\<\<\<\<\<\<\<\<
> 
> returns:
> 
> C:\A\ruby\>ruby w.rb  
> get\_week\_list(): Today’s day: 5  
> get\_week\_list(): 1. this\_week\_end: Thu Jul 09 12:00:00 -0700 2009  
> get\_week\_list(): 2. max\_date’s day: 4  
> get\_week\_list(): 3. max\_date: Thu Jun 11 13:14:44 -0700 2009
> 
> get\_week\_list(): 4. this\_dt: Thu Jul 09 12:00:00 -0700 2009  
> get\_week\_list(): 5. max\_dt: Thu Jun 11 13:14:44 -0700 2009  
> get\_week\_list(): 6. next\_dt: Thu Jul 02 12:00:00 -0700 2009
> 
> get\_week\_list(): 4. this\_dt: Thu Jul 02 12:00:00 -0700 2009  
> get\_week\_list(): 5. max\_dt: Thu Jun 11 13:14:44 -0700 2009  
> get\_week\_list(): 6. next\_dt: Thu Jun 25 12:00:00 -0700 2009
> 
> [[[0, 0, 12, 25, 6, 2009, 4, 176, true, “Pacific Daylight Time”]]]  
> Array

I have not looked into the reasons why your code doesn’t work, just took  
your post as a coding exercise. My try looks like this:

require ‘Date’  
class Date  
def thursdays\_upto\_now  
end\_date = Date.today  
res = []  
# get the last thursday:  
end\_date -= 1 while (end\_date.wday != 4)  
end\_date.step(self, -7) do |date|  
res \<\< date  
end  
res  
end  
end

start\_date = Date.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ )  
start\_date.thursdays\_upto\_now.each{|thursday| puts thursday.to\_s} # or  
whatever

I was happy finding out Date has a step method.

hth, I had some fun,

Siep

---

<div class="post-metadata">

**Author:** ![epasquier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2bfe46/32.png) [@epasquier](https://www.ruby-forum.com/u/epasquier)\
**Post date:** [July 13, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/calculating-an-array-of-dates/171890/3 "2009-07-13T17:42:20Z")

</div>

Siep K. wrote:

> I have not looked into the reasons why your code doesn’t work, just took  
> your post as a coding exercise. My try looks like this:
> 
> require ‘Date’  
> class Date  
> def thursdays\_upto\_now  
> end\_date = Date.today  
> res = []  
> # get the last thursday:  
> end\_date -= 1 while (end\_date.wday != 4)  
> end\_date.step(self, -7) do |date|  
> res \<\< date  
> end  
> res  
> end  
> end
> 
> start\_date = Date.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ )  
> start\_date.thursdays\_upto\_now.each{|thursday| puts thursday.to\_s} # or  
> whatever
> 
> I was happy finding out Date has a step method.
> 
> hth, I had some fun,
> 
> Siep

much better then my final code. I guess my mind is still stuck in  
FORTRAN method of coding. I’m finding Ruby very nice and interesting.  
Lots of new tricks to learn.

Thanks  
Eric

---

<div class="post-metadata">

**Author:** ![epasquier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2bfe46/32.png) [@epasquier](https://www.ruby-forum.com/u/epasquier)\
**Post date:** [July 13, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/calculating-an-array-of-dates/171890/4 "2009-07-13T19:37:42Z")

</div>

> today = Date.today  
> puts( today + 4 - today.wday)
> 
> …no need for the while with the -=. In fact won’t the -= always  
> give you the previous Thursday? The #step I didn’t know about. Good  
> advice.
> 
> I didn’t test the above code, but I’m pretty sure it will work for all  
> edge cases.

Sorry, should have been more clear. This is simply a demonstration of  
how to find the current week’s Thursday without a loop.

The rest of Siep’s code makes sense. I’d probably end up doing  
something crazy. In any case, the added method is probably obscure  
enough to not affect the base class Date in other applications.

Todd

---

<div class="post-metadata">

**Author:** ![epasquier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2bfe46/32.png) [@epasquier](https://www.ruby-forum.com/u/epasquier)\
**Post date:** [July 13, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/calculating-an-array-of-dates/171890/5 "2009-07-13T19:28:20Z")

</div>

On Mon, Jul 13, 2009 at 10:42 AM, Eric  
Peterson[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > res = []
> > 
> > ```
> > 
> > start\_date.thursdays\_upto\_now.each{|thursday| puts thursday.to\_s} # or  
> > FORTRAN method of coding. I’m finding Ruby very nice and interesting.  
> > Lots of new tricks to learn.  
> > …

today = Date.today  
puts( today + 4 - today.wday)

…no need for the while with the -=. In fact won’t the -= always  
give you the previous Thursday? The #step I didn’t know about. Good  
advice.

I didn’t test the above code, but I’m pretty sure it will work for all  
edge cases.

Todd
