question about working with dates

Discussion in 'Ruby' started by eggman2001, Apr 30, 2011.

  1. eggman2001

    eggman2001 Guest

    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.
    eggman2001, Apr 30, 2011
    #1
    1. Advertising

  2. eggman2001

    jake kaiden Guest

    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?


    - j

    --
    Posted via http://www.ruby-forum.com/.
    jake kaiden, Apr 30, 2011
    #2
    1. Advertising

  3. eggman2001

    7stud -- Guest

    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

    if date == first_day_of_month
    puts date
    end
    end

    --output:--
    2011-04-01
    2011-03-01
    2011-02-01
    2011-01-03
    2010-12-01
    2010-11-01
    2010-10-01
    2010-09-01
    2010-08-02
    2010-07-01
    2010-06-01
    2010-05-03
    2010-04-01
    2010-03-01
    2010-02-01
    2010-01-01
    2009-12-01
    2009-11-02
    2009-10-01
    2009-09-01
    2009-08-03
    2009-07-01
    2009-06-01
    2009-05-01
    2009-04-01
    2009-03-02
    2009-02-02
    2009-01-01
    2008-12-01
    2008-11-03
    2008-10-01
    2008-09-01
    2008-08-01
    2008-07-01
    2008-06-02
    2008-05-01
    2008-04-01
    2008-03-03
    2008-02-01
    2008-01-01
    2007-12-03
    2007-11-01
    2007-10-01
    2007-09-03
    2007-08-01
    2007-07-02
    2007-06-01
    2007-05-01
    2007-04-02
    2007-03-01
    2007-02-01
    2007-01-01
    2006-12-01
    2006-11-01
    2006-10-02
    2006-09-01
    2006-08-01
    2006-07-03
    2006-06-01
    2006-05-01

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 30, 2011
    #3
  4. eggman2001

    7stud -- Guest

    Also, it would be clearer to write something like this:

    results = []

    Saturday = 6
    Sunday = 7
    Weekend = [Saturday, Sunday]
    ....
    ....

    while Weekend.include?(first_day_of_month.wday)
    first_day_of_month += 1
    end

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 30, 2011
    #4
  5. eggman2001

    7stud -- Guest

    eggman2001 wrote in post #995956:
    >
    > - 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?

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 30, 2011
    #5
  6. eggman2001

    7stud -- Guest

    7stud -- wrote in post #995994:
    > 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
    >


    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.

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 30, 2011
    #6
  7. eggman2001

    7stud -- Guest

    Add year to Date object:
    Multiply by 12 and add that many months (see below)

    Subtract year from Date object:
    Multiply by 12 and add that many months (see below)

    Add month to Date object:
    new_date = date >> 1

    Subtract month from Date object:
    new_date = date << 1 (or *add* a negative int)

    Add day to Date object:
    new_date = date + 1

    Subtract day from Date object:
    new_date = date - 1

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, May 1, 2011
    #7
  8. eggman2001

    eggman2001 Guest

    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=A0pm, 7stud -- <> 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 =3D []
    > previous_years =3D 5
    >
    > 1.upto(previous_years * 365) do |i|
    > =A0 new_date =3D today - i
    >
    > =A0 next if [6,0].include?(new_date.wday)
    > =A0 date_arr << new_date
    > end
    >
    > #Print out the first weekday of each month:
    >
    > date_arr.each do |date|
    > =A0 year =3D date.year
    > =A0 month =3D date.month
    > =A0 first_day_of_month =3D Date.new(year, month, 1)
    >
    > =A0 while [6,0].include?(first_day_of_month.wday)
    > =A0 =A0 first_day_of_month +=3D 1
    > =A0 end
    >
    > =A0 if date =3D=3D first_day_of_month
    > =A0 =A0 puts date
    > =A0 end
    > end
    >
    > --output:--
    > 2011-04-01
    > 2011-03-01
    > 2011-02-01
    > 2011-01-03
    > 2010-12-01
    > 2010-11-01
    > 2010-10-01
    > 2010-09-01
    > 2010-08-02
    > 2010-07-01
    > 2010-06-01
    > 2010-05-03
    > 2010-04-01
    > 2010-03-01
    > 2010-02-01
    > 2010-01-01
    > 2009-12-01
    > 2009-11-02
    > 2009-10-01
    > 2009-09-01
    > 2009-08-03
    > 2009-07-01
    > 2009-06-01
    > 2009-05-01
    > 2009-04-01
    > 2009-03-02
    > 2009-02-02
    > 2009-01-01
    > 2008-12-01
    > 2008-11-03
    > 2008-10-01
    > 2008-09-01
    > 2008-08-01
    > 2008-07-01
    > 2008-06-02
    > 2008-05-01
    > 2008-04-01
    > 2008-03-03
    > 2008-02-01
    > 2008-01-01
    > 2007-12-03
    > 2007-11-01
    > 2007-10-01
    > 2007-09-03
    > 2007-08-01
    > 2007-07-02
    > 2007-06-01
    > 2007-05-01
    > 2007-04-02
    > 2007-03-01
    > 2007-02-01
    > 2007-01-01
    > 2006-12-01
    > 2006-11-01
    > 2006-10-02
    > 2006-09-01
    > 2006-08-01
    > 2006-07-03
    > 2006-06-01
    > 2006-05-01
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.
    eggman2001, May 2, 2011
    #8
  9. eggman2001

    7stud -- Guest

    >
    > 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.

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, May 2, 2011
    #9
  10. eggman2001

    eggman2001 Guest

    Okay thank you for your help.

    On May 2, 1:09=A0pm, 7stud -- <> 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? =A0 Did you look at the
    > output? =A0Is 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.
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.
    eggman2001, May 3, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    831
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    168
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    192
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    169
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    144
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page