I have a list (in database) of weekday non-holiday dates that goes
from present back to 1990. From this list, I'd like to pull out dates
given different kinds of criteria and add them to an array. Examples
of criteria are:
- first day of the month for the most recent 5 years. Perhaps obvious,
but the first weekday non-holiday of the month isn't necessarily day 1
(i.e. 5/1).
- first day of the month and one week after first day of the month for
the most recent 5 years. If one week after first day of the month
isn't on the list, then I'd like the closest day thereafter.
Any thoughts on how best to do this? Code snippets would be greatly
appreciated.
seems like you could make an array of arrays out of your dates, with the
index being the number of the month, or a hash with the month's name as
the key and an array of dates within the month as the value... that way
you could pull out the first index of each month and get the first
non-holiday weekday (not necessarily the 1st of the month, as you
mentioned,) and add 7 to get the next non-holiday weekday.
have you got some code you're working with that you could post?
I have a list (in database) of weekday non-holiday dates that goes
from present back to 1990. From this list, I'd like to pull out dates
given different kinds of criteria and add them to an array. Examples
of criteria are:
- first day of the month for the most recent 5 years. Perhaps obvious,
but the first weekday non-holiday of the month isn't necessarily day 1
(i.e. 5/1).
All it takes is a little math:
require 'date'
#Create array of all weekday dates for last five years
#(probably faster than pulling them out of a database):
date_arr =
previous_years = 5
1.upto(previous_years * 365) do |i|
new_date = today - i
next if [6,0].include?(new_date.wday)
date_arr << new_date
end
#Print out the first weekday of each month:
date_arr.each do |date|
year = date.year
month = date.month
first_day_of_month = Date.new(year, month, 1)
while [6,0].include?(first_day_of_month.wday)
first_day_of_month += 1
end
- first day of the month and one week after first day of the month for
the most recent 5 years. If one week after first day of the month
isn't on the list, then I'd like the closest day thereafter.
Isn't that just adding 7 days to each of the dates produced by your
first criteria?
I have a list (in database) of weekday non-holiday dates that goes
from present back to 1990. From this list, I'd like to pull out dates
given different kinds of criteria and add them to an array. Examples
of criteria are:
- first day of the month for the most recent 5 years. Perhaps obvious,
but the first weekday non-holiday of the month isn't necessarily day 1
(i.e. 5/1).
All it takes is a little math:
require 'date'
#Create array of all weekday dates for last five years
#(probably faster than pulling them out of a database):
date_arr =
previous_years = 5
Hmmmm...looks like the following line got deleted somehow:
today = Date.today
···
1.upto(previous_years * 365) do |i|
new_date = today - i
next if [6,0].include?(new_date.wday)
date_arr << new_date
end
========
You could benchmark whether adding the check below would be faster:
last_result = results[-1]
if last_result.is_a?(Date) and last_result.month == month
next
end
This would be better:
last_result = results[-1]
begin
next if last_result.month == month
rescue NoMethodError #do nothing
end
That rescue handles the initial case when the results array is empty,
and results[-1] returns nil. After the array has one element, then only
the if check will be executed.
This is very helpful. The only thing is that I'd like to use my
existing list of dates (for certain reasons) as opposed to creating a
new list.
My list includes only weekday dates, so I'm not concerned about
weekend/weekday so long as I'm using this list. I'd like to pull out
the first day of each month from my list. Any chance you could give me
an idea of how to do this?
Thanks much.
···
On Apr 30, 3:41 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
eggman2001 wrote in post #995956:
> I have a list (in database) of weekday non-holiday dates that goes
> from present back to 1990. From this list, I'd like to pull out dates
> given different kinds of criteria and add them to an array. Examples
> of criteria are:
> - first day of the month for the most recent 5 years. Perhaps obvious,
> but the first weekday non-holiday of the month isn't necessarily day 1
> (i.e. 5/1).
All it takes is a little math:
require 'date'
#Create array of all weekday dates for last five years
#(probably faster than pulling them out of a database):
date_arr =
previous_years = 5
1.upto(previous_years * 365) do |i|
new_date = today - i
next if [6,0].include?(new_date.wday)
date_arr << new_date
end
#Print out the first weekday of each month:
date_arr.each do |date|
year = date.year
month = date.month
first_day_of_month = Date.new(year, month, 1)
while [6,0].include?(first_day_of_month.wday)
first_day_of_month += 1
end
My list includes only weekday dates, so I'm not concerned about
weekend/weekday so long as I'm using this list. I'd like to pull out
the first day of each month from my list. Any chance you could give me
an idea of how to do this?
lol. What do you think the programs I posted do? Did you look at the
output? Is it your opinion that those are random dates I posted?
Really, if you don't know enough ruby to even read an answer, you
shouldn't be asking questions on a ruby forum--you are just wasting
everyone's time.
On May 2, 1:09 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
> My list includes only weekday dates, so I'm not concerned about
> weekend/weekday so long as I'm using this list. I'd like to pull out
> the first day of each month from my list. Any chance you could give me
> an idea of how to do this?
lol. What do you think the programs I posted do? Did you look at the
output? Is it your opinion that those are random dates I posted?
Really, if you don't know enough ruby to even read an answer, you
shouldn't be asking questions on a ruby forum--you are just wasting
everyone's time.