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.

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.

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.";
@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;

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";"") +

This was first published in April 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.