Forum: Ruby Newbie: dates by week and year

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeroen van Doorn (Guest)
on 2006-04-07 18:21
(Received via mailing list)
Hi Everybody,

Am a newbie to ruby, so forgive me if this is a stupid question,

I'm looking for a way to get  a series of dates (monday - sunday or
sunday - saturday) , but I'm only having
the year and the week number...

Hopefully someone can help,
Kind regards,
Jeroen van Doorn
Reid T. (Guest)
on 2006-04-07 18:36
(Received via mailing list)
Jeroen van Doorn wrote:
> Jeroen van Doorn
>
http://www.rubycentral.com/book/ref_c_time.html#Ti...
unknown (Guest)
on 2006-04-07 18:42
(Received via mailing list)
On Fri, 7 Apr 2006, Jeroen van Doorn wrote:

> Jeroen van Doorn
this ought to get you going:

     harp:~ > cat a.rb
     require 'date'

     def weekno year, week
       jjj = (7 * (week - 1)) + 1
       jd = Date.ordinal_to_jd year, jjj
       yyyy, m, d = Date.jd_to_civil jd
       a = Date.new yyyy, m, d
       b = Date.new yyyy, m, d + 7
       (a ... b)
     end

     first = weekno 2006, 1
     p first.map{|day| day.to_s}

     second = weekno 2006, 2
     p second.map{|day| day.to_s}

     third = weekno 2006, 3
     p third.map{|day| day.to_s}



     harp:~ > ruby a.rb
     ["2006-01-01", "2006-01-02", "2006-01-03", "2006-01-04",
"2006-01-05", "2006-01-06", "2006-01-07"]
     ["2006-01-08", "2006-01-09", "2006-01-10", "2006-01-11",
"2006-01-12", "2006-01-13", "2006-01-14"]
     ["2006-01-15", "2006-01-16", "2006-01-17", "2006-01-18",
"2006-01-19", "2006-01-20", "2006-01-21"]


regards.

-a
Jeroen van Doorn (Guest)
on 2006-04-10 12:16
(Received via mailing list)
Thanks for the answer!

This one really gave me a headache ...

Kind regards,
Jeroen van Doorn
This topic is locked and can not be replied to.