## Getting number of days in a month -- getting the number of weeks in a year.

### Getting number of days in a month -- getting the number of weeks in a year.

A more interesting question is how do you get the number of weeks in a
year.
Some years have 52...some have 54. Calendar weeks...not 7 day weeks.

S

### Getting number of days in a month -- getting the number of weeks in a year.

I assume you mean here how many 7 day weeks starting on a particular
week day have at least one day in a given year.

If this is the case then in all but one case every year has 53
'weeks.' The only exception is a leap year which starts on the last
day of the week which has 54 weeks.

for start in (1..7) do
for days in (365..366) do
s = "a #{{365 => 'normal', 366 => 'leap'}[days]} year starting on
weekday #{start} starts with "
start_week_days = start == 1 ? 0 : (8 - start)
s << "a 'week' of #{start_week_days} days then " unless start_week_days == 0
days_remaining = days - start_week_days
full_weeks = days_remaining / 7
end_week_days = days_remaining % 7
s << "#{full_weeks} full weeks"
s << " and ends with a 'week' of #{end_week_days} days" unless
end_week_days == 0
s << " a total of #{(start_week_days > 0 ? 1 : 0) + full_weeks +
(end_week_days > 0 ? 1 : 0)} 'weeks.'"
puts s
end
puts
end

a normal year starting on weekday 1 starts with 52 full weeks and ends
with a 'week' of 1 days a total of 53 'weeks.'
a leap year starting on weekday 1 starts with 52 full weeks and ends
with a 'week' of 2 days a total of 53 'weeks.'

a normal year starting on weekday 2 starts with a 'week' of 6 days
then 51 full weeks and ends with a 'week' of 2 days a total of 53
'weeks.'
a leap year starting on weekday 2 starts with a 'week' of 6 days then
51 full weeks and ends with a 'week' of 3 days a total of 53 'weeks.'

a normal year starting on weekday 3 starts with a 'week' of 5 days
then 51 full weeks and ends with a 'week' of 3 days a total of 53
'weeks.'
a leap year starting on weekday 3 starts with a 'week' of 5 days then
51 full weeks and ends with a 'week' of 4 days a total of 53 'weeks.'

a normal year starting on weekday 4 starts with a 'week' of 4 days
then 51 full weeks and ends with a 'week' of 4 days a total of 53
'weeks.'
a leap year starting on weekday 4 starts with a 'week' of 4 days then
51 full weeks and ends with a 'week' of 5 days a total of 53 'weeks.'

a normal year starting on weekday 5 starts with a 'week' of 3 days
then 51 full weeks and ends with a 'week' of 5 days a total of 53
'weeks.'
a leap year starting on weekday 5 starts with a 'week' of 3 days then
51 full weeks and ends with a 'week' of 6 days a total of 53 'weeks.'

a normal year starting on weekday 6 starts with a 'week' of 2 days
then 51 full weeks and ends with a 'week' of 6 days a total of 53
'weeks.'
a leap year starting on weekday 6 starts with a 'week' of 2 days then
52 full weeks a total of 53 'weeks.'

a normal year starting on weekday 7 starts with a 'week' of 1 days
then 52 full weeks a total of 53 'weeks.'
a leap year starting on weekday 7 starts with a 'week' of 1 days then
52 full weeks and ends with a 'week' of 1 days a total of 54 'weeks.'

Or did you mean something else.

--
Rick DeNatale

My blog on Ruby
http://www.yqcomputer.com/

### Getting number of days in a month -- getting the number of weeks in a year.

ood job. That's exactly what I meant.

On Feb 28, 2008, at 9:01 PM, Rick DeNatale wrote:

### Getting number of days in a month -- getting the number of weeks in a year.

(...)

Following this analysis:

require 'date'

def numweeks(year)
start_year = Date.civil(year,1,1)
end_year = Date.civil(year,-1,-1)
(start_year.strftime("%U") .. end_year.strftime("%U")).to_a.size
end

puts numweeks(2028) # =>54

regards,

Siep

--
Posted via http://www.yqcomputer.com/

### Getting number of days in a month -- getting the number of weeks in a year.

Well, that doesn't exactly follow my analysis, but it does work, but
only if you define the week to start on Sunday.

Another way which DOES follow my analysis

def leap?(year)
year % 400 == 0 || year % 4 == 0 && year % 100 != 0
end

def numweeks(year, start_wday = 0)
leap?(year) && Date.civil(year,1,2).wday == start_wday ? 54 : 53
end

As it turns out, rather unsurprisingly given the world in which we
live, numbering the weeks in a year is more complicated than counting
them.

http://www.yqcomputer.com/ #Week_number
http://www.yqcomputer.com/
--
Rick DeNatale

My blog on Ruby
http://www.yqcomputer.com/

### Getting number of days in a month -- getting the number of weeks in a year.

That's why i like this group. You learn a lot and get some good Ruby
code.