From: Rick DeNatale Date: 2008-02-29T23:32:14+09:00 Subject: Re: Getting number of days in a month -- getting the number of weeks in a year. On 2/29/08, Siep Korteling wrote: > Rick Denatale wrote: > > On 2/28/08, Serg Koren wrote: > > > (...) > > > 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.' > > > > (...) > > 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 > 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://en.wikipedia.org/wiki/Week#Week_number http://en.wikipedia.org/wiki/ISO_week_date -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/