Skip to content

Instantly share code, notes, and snippets.

@lpar
Created June 17, 2011 18:28
Show Gist options
  • Save lpar/1031977 to your computer and use it in GitHub Desktop.
Save lpar/1031977 to your computer and use it in GitHub Desktop.
ISO 8601 week number calculation in LotusScript
Function WeekNumber(t As NotesDateTime) As String
' Wrapper to work out week numbers of NotesDateTime objects in the
' current time zone
Dim lst As Variant
lst = t.LSLocalTime
WeekNumber = LSWeekNumber(lst)
End Function
Function LSWeekNumber(t As Variant) As String
' Returns the week number of a NotesDateTime object
' computed in the local time zone according to ISO 8601
' which states that week 1 is the week containing the first Thursday
' (or, equivalently, the week containing January 4th).
' Returns a 7-character string; example: "2010W05"
' First, strip any time information and get year, month, day, and day-of-week
Dim lst As Variant
lst = Int(t)
Dim yyyy As Integer
yyyy = Year(lst)
Dim mm As Integer
mm = Month(lst)
Dim dd As Integer
dd = Day(lst)
dow = Weekday(lst)
' The first special cases are when the first week of the next year
' protrudes back into this year and we've been asked to compute week number
' for a date right at the end of the year
If (mm = 12) Then
If (dd >=29 And dow = 2) Or (dd >= 30 And dow = 3) Or (dd = 31 And dow = 4) Then
LSWeekNumber = Trim(Str(yyyy)) & "W" & "01"
Exit Function
End If
End If
' OK, not a special case, so we have some work to do
' Create a date number for the first day of the year
Dim fdoy As Variant
fdoy = Datenumber(yyyy, 1, 1)
' Work out what day of the week that was
' Note that Notes uses Sunday = 1, which is non-standard as per ISO 8601
Dim fdow As Integer
fdow = Weekday(fdoy)
' Compute offset
Dim offset As Integer
If (fdow > 5) Then
offset = fdow - 9
Else
offset = fdow - 2
End If
' Compute week number
Dim wn As String
wn = Right("0"&Trim(Str(Int(((lst + offset) - fdoy) / 7) + 1)), 2)
If (wn = "00") Then
' It's the last week number of the previous year,
' i.e. the week number it was on December 31st of that year
' so call ourselves recursively to compute that
wn = Right(LSWeekNumber(Datenumber(yyyy-1, 12, 31)), 2)
LSWeekNumber = Trim(Str(yyyy - 1)) & "W" & wn
Exit Function
End If
LSWeekNumber = Trim(Str(yyyy)) & "W" & wn
End Function
@Lars-Berntrop-Bos-SKG
Copy link

There's a bug at row 30 the correct is LSWeekNumber = Trim( Str( yyyy + 1 )) & "W" & "01" in fact 31/12/2018 return 2019W01

Read ISO 8601: 2018/12/31 IS in the first week of 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment