# Converting a date to a week number and vice versa

## Formula

### Looking for something else?

PRODUCTS
IBM Lotus Notes LotusScript

This tip shows you how to convert a date to a week number and vice versa using Formula Language. It was written at R4, and I can no longer remember why it works -- but it does.

The International Organization for Standardization defines Week 1 in a given year as the first week (starting Monday) that contains at least 4 January-days. In other words, Week 1 is the first week that includes a Thursday.

```
Code: Date -> Week Number:
"SomeDate" is a date-time field
containing a date.

Jan1st := @Date(@Year(SomeDate); 1; 1);
REM {Jan 1st same same year
(not necessarily part of week 1)};
Adjust := 3 - @Modulo((@Weekday(Jan1st) + 1); 7);
REM {Number of days to Monday week 1};
Adjust;0; 0; 0); REM {Date for Monday week 1};
WeekNumber := 1 +
@Integer(((SomeDate - MondayW1)\86400)\7)

Note that LotusScript can return a
Week Number for a Date directly.

Week Number -> Date
"SomeYear" is a numerical field
containing a year.
"SomeWeek" is a numerical field
containing a week number.
"SomeWeekday" is a numerical field
containing a weekday number (Monday=1, Sunday =7)

Jan1st := @Date(SomeYear; 1; 1);
REM {Jan 1st same year
(not necessarily part of week 1)};
@Modulo((@Weekday(Jan1st) + 1); 7);
REM {Number of days to "SomeWeekday" week 1};
Adjust;0; 0; 0); REM {Date for "SomeWeekday" week 1};
Date := @Adjust(SomeWeekdayW1; 0; 0; 7 *
(SomeWeek - 1); 0; 0; 0)
```

Klavs Thor Olesen says in in his tip that LotusScript can return a Week Number for a Date directly.

I would like to know how, because I have not found any method that does this.
Goran L.

******************************************

It is the Format function I referred to. Here is an example:

```Sub Click(Source As Button)
Msgbox Format(Today, "ww"), 0+64+0+0, "Week number of Today"
End Sub```

Klavs Thor Olesen, tip author

******************************************

This is exactly what I was looking for. I was looking for a formula to calculate a week number field on a form based on a date entered in another field. I typed in calculate week number in a search engine and there it was, number eight in my search results. It's just what I was looking for and it does exactly what I need it to do. I set the properties of the new field to compute after validation and it works perfectly. Thanks very much.
Bob V.

******************************************

Here is the same function converted to LotusScript:

```--Code start--
Function fWeekNumberDate
(numYear As Integer, numWeekNo As
Integer, numWeekDay As Integer ) As Variant
%REM
Version 1.0
Author: Andrew Dempster, Constellation
Europe Ltd
Date: 28/06/2007
Cobbled together from various places
this script is a duplicate of a @Formula
that returns a date from a week number and day

"numYear" is a numerical value containing
a year.
"numWeekNo" is a numerical value containing
a week number.
"numWeekDay" is a numerical value containing
a weekday number (Sunday=1, Monday=2, Saturday =7)

%END REM
Dim dtJan1st As NotesDateTime
Dim dtSomeWeekDayW1
As NotesDateTime

Set dtJan1st = New NotesDateTime
("01/01/" & Cstr(numYear))
'/* Jan 1st same year
(not necessarily part of week 1)
numAdjust = numWeekDay + 1 -
(Weekday(dtJan1st.DateOnly)+1) Mod 7
'/* Number of days to "numWeekDay" week 1
Set dtSomeWeekDayW1 =
New NotesDateTime(dtJan1st.DateOnly)
'/* Date for "numWeekDay" week 1
(7 *(numWeekNo -1))
fWeekNumberDate =
dtSomeWeekDayW1.DateOnly
End Function
--Code end--
```

Also, here's a handy hint for the Formula code, using correct weekdays:

```--Code start--
N := "1" : "2" : "3" : "4" : "5" : "6" : "7";
Days := "Sunday" : "Monday" : "Tuesday"
: "Wednesday" : "Thursday" : "Friday"
: "Saturday";
SomeWeekday := @TextToNumber
(@Replace(Day;Days;N)); <--
Where Day is your Text date field
Adjust := SomeWeekday + 1 -
@Modulo((@Weekday(Jan1st) + 1); 7);
<- Changed to +1 as days now in line
with @WeekDay function!
--Code end--
```

I hope this helps (I've got a calendar population routine in the pipeline that I shall be submitting later). A huge thanks to Klavs for this function; it's a lifesaver!
Andrew D.

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip library by member Klavs Thor Olesen. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.

This was last published in January 2006

#### Start the conversation

Send me notifications when other members comment.

## SearchWindowsServer

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

• ### Microsoft rings in new year with light January Patch Tuesday

Microsoft starts 2017 with just three bulletins, but administrators who manage Windows Server 2008 and 2008 R2 should pay close ...

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

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

• ### Avoid disaster with these Exchange 2013 backup options

Exchange Server administrators have a number of ways to keep disaster from sinking a key part of the corporate infrastructure.

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