Lotus Notes FAQ


How do you calculate the week number in a year of a given date?

This formula satisfies ISO 8601:1988:

REM "Replace D with the date of interest.";
D := [12/31/95];
FirstOfYear := @Date(@Year(D); 1; 1);
LastOfYear := @Date(@Year(D); 12; 31);
FirstDayNum := @Weekday(FirstOfYear);
LastDayNum := @Weekday(LastOfYear);
REM "ISO weeks start on Monday and ends on Sunday.";
ISOFirstDayNum := @If(FirstDayNum = 1; 7; FirstDayNum - 1);
ISOLastDayNum := @If(LastDayNum = 1; 7; LastDayNum - 1);
REM "The first and last ISO week is the first";
REM "and last ISO week to include Thursday"; 
IsFirstWeek := 7 - ISOFirstDayNum > 2;
IsLastWeek := 7 - ISOLastDayNum < 4;
REM "The date of the first day of the first ISO week";
ISOFirstDay := @If(IsFirstWeek;
            @Adjust(FirstOfYear; 0; 0; 1 - ISOFirstDayNum; 0; 0; 0);
            @Adjust(FirstOfYear; 0; 0; 8 - ISOFirstDayNum; 0; 0; 0));
REM "The date of the last day of the last ISO week";
ISOLastDay := @If(IsLastWeek;
            @Adjust(LastOfYear; 0; 0; 7 - ISOLastDayNum; 0; 0; 0);
   @Adjust(LastOfYear; 0; 0; -ISOLastDayNum; 0; 0; 0));
REM "Date outside ISOFirstDay and ISOlastDay";
REM "are from the previous or next year";
REM "Return the ISO week number and exit";
FirstWeekNextYear := @If(@Day(D) > @Day(ISOLastDay) 
                       & @Year(D) = @Year(ISOLastDay);
            @Return(@Text(@Year(D)+1)+"W01"); NULL);
REM "I suspect this is where Julian dates would be useful";
REM "A recursive call could be used in a real language";
LastYear := (D - @Adjust(FirstOfYear; -1; 0; 0; 0; 0; 0))/60/60/24/7;
LastWeekLastYear := @If(@Day(D) < @Day(ISOFirstDay);
                       & @Year(D) = @Year(ISOFirstDay);
            @Return(@Text(@Year(D) - 1)+"W"+@Text(LastYear)); NULL);
REM "If you get this far, the date falls into an ISO week this year";
REM "Convert the difference in seconds to weeks";
NumWeeks := (D - ISOFirstDay)/60/60/24/7;
REM "Fractions indicate that the date falls";
REM "in the middle of the ISO week";
WeekAdjust := 1 - (NumWeeks - @Integer(NumWeeks));
ISOWeekNum := NumWeeks + WeekAdjust;
REM "Conform to ISO 8601 format";
Pad:=@If(ISOWeekNum<10;"0";"");
@Text(@Year(D))+"W"+Pad+@Text(ISOWeekNum)


Home *Notes V3 *Notes V4 *Notes/Domino 4.5

Last Modified: July 28, 1997