# 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.

### Looking for something else?

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

```
This was last published in April 2002

#### Start the conversation

Send me notifications when other members comment.

## SearchWindowsServer

• ### Windows Management Framework 5 update gives PowerShell users a fix

Microsoft answered admin requests for new features and fixed several bugs with its WMF 5.1 release, giving users a slew of ...

• ### Pester PowerShell unit testing ensures quality code

PowerShell jockeys can turn to the Pester unit testing framework to ensure the code they write is up to snuff.

• ### Microsoft software-defined networking gets baked into Windows Server

The Microsoft SDN technology that comes in the Windows Server 2016's Datacenter edition features a range of basic to nuanced ...

## Search400

• ### iSeries tutorials

Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

• ### V6R1 upgrade planning checklist

When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

• ### Connecting multiple iSeries systems through DDM

Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

## SearchEnterpriseLinux

• ### SUSE Linux jumps on the open private cloud deployment train

SUSE Linux Enterprise planning private cloud deployment with OpenStack weight behind it. NVIDIA works on Linux drivers for ...

• ### Want one of the many Linux jobs out there? Time to go back to class

Does extended education from the Linux Foundation and others help graduates meet the demands of today’s Linux jobs? Some say yes.

• ### Ubuntu ARM server AMI for Amazon EC2 offers fast access in the cloud

Canonical’s Ubuntu ARM server AMI for Amazon EC2 provide open source shops with a fast ARM server for the cloud.

## SearchDataCenter

• ### HPE-SimpliVity deal raises support, price and development questions

With HPE's buy of No. 2 SimpliVity -- the first big deal in the HCI space -- IT pros see a more robust offering, but also higher ...

• ### Converged infrastructure drop-off doesn't mean data center death

Traditional converged infrastructure has been supplanted by hyper-converged infrastructure and cloud computing, but it remains a ...

• ### New options to evolve your data backup and recovery plan

The server backup market first evolved to protect VMs, but now it's undergoing another transformation. Find out how it's evolved ...

## SearchExchange

• ### Beware of these six Office 365 cons for IT shops accustomed to on premises

Office 365 offers many benefits, but there are plenty of cons to consider before leaping to this cloud-based productivity suite ...

• ### Five reasons for an Office 365 hybrid deployment

Not all organizations can go all-in on cloud. There are five reasons why a hybrid Office 365 arrangement makes sense for those ...

Admins must keep a close eye on Exchange Server to ensure it runs at peak performance. If cost is an issue, there are a number of...

## SearchContentManagement

• ### Why SharePoint Framework could benefit business, not just developers

SharePoint Framework heralds a new, mobile-friendly, low-code era for Microsoft, which may give some organizations without .NET ...

• ### Using Microsoft MyAnalytics to improve workers' time management

Microsoft MyAnalytics has been hailed as the next great thing for analyzing worker productivity. But how can employees be assured...

• ### Is the Drupal content management system right for your business?

The open source Drupal content management system offers users responsive design and WYSIWYG content and web development tools, as...

Close