Getting the week number of a given date in lotus notes @formulas

27.07.2005 at 12:42

Ok first of all i am one of those unlucky guys who has to develop things with Lotus Domino. Especially the formula language is kind of crap (my personal opinion).

But anyway i had to program a calendar and therefore i needed to get the week number of a given date. Thanks to the notesweb where i found an almost working version, i just needed to patch it a bit. Here is my final code which seems to work (at least for my purpose).

REM { returns the week number from a given date };

Date:=@Date(2005; 07; 27);

FirstOfYear := @Date(@Year(Date); 1; 1);
LastOfYear := @Date(@Year(Date); 12; 31);
ISOFirstDayNum := @Weekday(FirstOfYear);
ISOLastDayNum := @Weekday(LastOfYear);	

REM {
   The first and last ISO week is the first
   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};
LastWeekLastYear := (Date - @Adjust(FirstOfYear;-1;0;0;0;0;0))/60/60/24/7;
AdjustLastWeek := 1 - (LastWeekLastYear - @Integer(LastWeekLastYear));
@Set("LastWeekLastYear"; LastWeekLastYear + AdjustLastWeek);

REM {If you get this far, the date falls into an ISO week this year};
REM {Convert the difference in seconds to weeks};
NumWeeks := (Date - ISOFirstDay)/60/60/24/7;

REM {Fractions indicate that the date falls in the middle of the ISO week};
ISOWeekNum:=@If(NumWeeks != @Integer(NumWeeks);@Integer(NumWeeks)+1;NumWeeks);
Pad:=@If(ISOWeekNum<10;"0";"");

@Prompt([OK];"Weeknumber";"Week " + @Text(Pad+@Text(ISOWeekNum)));

REM { Prints "Week 30" };

Comments (2)

WHAT?

30.05.2013 by TB

Lotus notes Formula is soooo great. :)

Excellent Formula

22.09.2015 by mike (mikebruinsma[at]hotmail[dot]com)

Easy to use, Many thanks Lotus Notes rules

1