Week number

Michael D Mays mdmays at airmail.net
Mon Apr 15 06:21:01 EDT 2002


Weeks are always seven days or 604800 seconds.

You can find the seconds for the first day of any year by doing this
(theYear is the year you are working in)
  put theYear&",1,1,0,0,0" into theYear
  convert theYear to seconds

Next you can find the seconds for the date you have in question (theDay is
the day in question and theMonth is the month in question)
  put theYear&","&themonth&","&theDay&","0,0,0" into theDate
  convert theDate to seconds

Now figure out how many weeks between the first day of the year and the date
you have in question
  put ((theDate-theMonth) div 604800) +1 into theWeeks

If the first day of the year is a Mon then the above calculates a week from
Mon to Sun as a week. If the first day of the year is a Thus the above
calculates a week from Thus to Wed and so on.

If you want the week to begin on a particular day you need to know the day
of the week the year begins. The following handler is 'hard wired' for a Mon
to Sun week and uses the above information to calculate the week number:

on mouseUp
  ask "What date?"
  convert it to dateItems
  put it into yearStart
  put "1,1" into item 2 to 3 of yearStart
  convert yearStart to dateItems
  put (item 7 yearStart-1)* 86400 into dayDif
  convert yearStart to seconds
  convert it to seconds
  answer "Week"&&(it-yearStart+dayDif)/86400  div 7 +1&&"of the year."
end mouseUp

If you want to change the day the week starts then a more general solution
for dayDif is needed.

You don't need to worry about leap year or how many days are in a month. The
convert function and the length of a week in seconds takes care of this
naturally. 

michael

yves COPPE of yvescoppe at skynet.be wrote the following on 4/14/02 3:34 AM

>> This is my last whack at it.. there's definitely got to be a better
>> way. But these seems to work.
>> Just specify your month, day & year in the first few lines...
>> 
>> on mouseup
>>  put the seconds into nowSecs
>>  put "12" into theMonth
>>  put "31" into theDay
>>  put "2002" into theYear
>>  put "no" into leapThisYear
>>  if "." is not in theYear/4 then
>>   put "yes" into leapThisYear
>>   if "." is in theYear/100 then
>>    put "no" into leapThisYear
>>    if "." is not in theYear/2000 then
>>     put "yes" into leapThisYear
>>    end if
>>   end if
>>  end if
>> if theMonth>01 then add 31 to dayCount
>> if th




More information about the use-livecode mailing list