Making a monthly calendar

On Tue, Mar 4, 2008 at 7:34 AM, Mikkel B. [email protected] wrote:

Mikkel
That is weird! Not only that, but for (2008, 6, 1), it gives me a
month of 2 weeks, the second having only 2 days.

Todd

On Tue, Mar 4, 2008 at 10:31 AM, Todd B. [email protected]
wrote:

really wierd

Mikkel

That is weird! Not only that, but for (2008, 6, 1), it gives me a
month of 2 weeks, the second having only 2 days.

Never mind that. It’s just 1 week.

Todd

Todd B. wrote:

On Mon, Mar 3, 2008 at 2:37 PM, Jeremy McAnally
[email protected] wrote:

There’s a calendar_helper plugin if this is for a Rails app. Works well for me.

–Jeremy

I agree. If this is to be productive, then you probably don’t have to
reinvent the wheel :slight_smile:

Todd

Yes. Here is my effort anyway:

require ‘date’
require ‘enumerator’

def days_to_show(year, month, num_week_rows)

returns an array of the dates in month,

with the last days of the preceding month

and the first days of the next month

first_of_month = Date.new(year, month, 1)
first = first_of_month - (first_of_month.wday)+1

+1 for weeks starting on monday, remove for weeks starting on sunday

last = first + (num_week_rows * 7)
(first…last).to_a
end

days = days_to_show(2008, 3, 6)
days.each_slice(7) do |week|
print "| "
week.each{|day| print day.to_s + "\t| "}
puts
end

The only thing noteworthy is the range of dates, which produces an
array.

Regards,

Siep

Behaves better.
Regards,

Siep

amazing…a correct and veru elegant solution…

thanks alot

Mikkel

Mikkel B. wrote:
(…)

require ‘date’
require ‘enumerator’

def days_to_show(year, month, num_week_rows)

returns an array of the dates in month,

with the last days of the preceding month

and the first days of the next month

first_of_month = Date.new(year, month, 1)
first = first_of_month - (first_of_month.wday)+1

+1 for weeks starting on monday, remove for weeks starting on sunday

last = first + (num_week_rows * 7)
(first…last).to_a
end

days = days_to_show(2008, 3, 6)
days.each_slice(7) do |week|
print "| "
week.each{|day| print day.to_s + "\t| "}
puts
end

(…)

What happens when you run ays = days_to_show(2008, 6, 6) ??? it shows
june as starting on sunday 2 of june…

really wierd

Mikkel

Argh… Date#wday gives with a zero for sunday. But there is good luck to
be found in ruby. With Date#cwday Monday is commercial day-of-week 1;
Sunday is commercial day-of-week 7. Fine by me.

require ‘date’
require ‘enumerator’

def days_to_show(year, month, num_week_rows = 6)

returns an array of the dates in month,

with the last days of the preceding month

and the first days of the next mont

first_of_month = Date.new(year, month, 1)
first = first_of_month - first_of_month.cwday + 1

+1 for weeks starting on monday, remove for weeks starting on sunday

last = first + num_week_rows * 7
(first…last).to_a
end

1.upto(12) do |month|
days = days_to_show(2008,month)
days.each_slice(7) do |week|
week.each{|day| print "| " + day.mday.to_s + “\t”}
puts “|”
end
puts
end

Behaves better.
Regards,

Siep