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

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/

ood job. That's exactly what I meant.

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

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

(...)

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/

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/

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

code.

code.

1. Outlook show week number(1-52); day of year / days left in year

2. Why won't the number 9 enter as that and gets replaced with 0 ins

3. Calculating the number of days/weeks/months/years that have pa

4. Calculating the number of days/weeks/months/years that have passed

5. Creating a date from week number, day number and year value

6. how do I convert a number to number of years, months and days

7. regexp for reg language that has an even number of a's and b's

8. Finding the Value (of text and numbers) between N/A's

9. How do I convert a number into years and months if the number represents months?

10. How do I convert a number into years and months if the number represents months?

11. Get day name given year, month and day number

12. how do i convert a number of days to years, months & days?

13. convert a number of days to years, months & days

14. how do I show day number / number of days remaining in the year?

15. hOW DO I CHANGE THE FROM MONTH/DAY/YEAR TO DAY/MONTH/YEAR?

6 post • Page:**1** of **1**