Formula to compute calendar week
Here is my formula to compute the calendar week number for a given date. This can be very useful in View columns.
Here is my formula to compute the calendar week number for a given date. This can be very useful in View columns.
The rule I am using is: the first week of the year is the week with at least 4 days of the new year. In other words, it is the week, which contains the first Thursday of the year.



Download: IT Certifications 101
Inside this exclusive essential guide, our independent experts break down which IT certifications are worth your time and effort, and how to get started obtaining them to further your career— including specific certifications that any cloud or desktop pro should seriously consider.
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.
REM "First compute week day Mo: 1 till Su: 7, be careful here if your settings are Su: 1 till Sa: 7";
tmpWeekday := 7-@Modulo(8-@Weekday(Datum);7);
REM "Compute Date Offset to Thursday";
tmpOffsetToThursday := tmpWeekday - 4;
REM "the result is a number
between -3 and +3 -> Mo: -3 und Su: +3";
tmpActThursday := @Adjust(Datum; 0; 0; - tmpOffsetToThursday; 0; 0; 0);
REM "Using this offset, compute the date of the corresponding Thursday and the corresponding year -> this gives us the 1.January, to start from, for the computation of the
calendar week";
tmpJan1rstRef := @Date(@Year(tmpActThursday); 1; 1);
REM "Find out the Thursday corresponding to the week of the 1st January computed above.";
tmpOffsetTo1stThu := 7-@Modulo(8-@Weekday(tmpJan1rstRef);7) - 4;
tmp1stThur := @Adjust(tmpJan1rstRef; 0; 0; - tmpOffsetTo1stThu; 0; 0; 0);
REM "If the found Date is in December, then take the date one week after to get the first week of the year.";
@Set("tmp1stThur";
@If(@Month(tmp1stThur)=12;@Adjust(tmp1stThur;0;0;7;0;0;0); tmp1stThur));
REM "Now, we have the Thursday of the week we want to get the number from and the first Thursday of the corresponding year. Just subtract the one from the other and convert from Seconds to days and weeks.";
tmpKW := ((tmpActThursday - tmp1stThur) /(24*3600) / 7) + 1;
tmpKW
________________
REM "And here is the contracted version of the code";
tmpActThursday := @Adjust(Datum; 0; 0; @Modulo(8-@Weekday(Datum);7) -
3; 0; 0; 0); tmpJan1rstRef := @Date(@Year(tmpActThursday); 1; 1); tmp1stThur := @Adjust(tmpJan1rstRef; 0; 0; @Modulo(8-@Weekday(tmpJan1rstRef);7)-3; 0; 0; 0); tmpKW := (((tmpActThursday - @If(@Month(tmp1stThur)=12;@Adjust(tmp1stThur;0;0;7;0;0;0); tmp1stThur)) /(24*3600) / 7) + 1); @Text(@Year(tmpJan1rstRef)) + " / " + @If(tmpKW<10;"0";"") +
@Text(tmpKW)
Start the conversation
0 comments