How to get Earliest or Latest Date for a Field from x Number of Possibly Filled Fields

Mindwatering Incorporated

Author: Tripp W Black

Created: 11/22/1999 at 08:52 PM

 

Category:
Notes Developer Tips
Formulas

Issue:
With list of dates, need to get earliest or latest dates.

Solutions:

LotusScript Example of LATEST Date.

%REM
    Function GetLatestDate
    Description: Checks list of dates to see which date is the latest using as string list of dates
%END REM
Function GetLatestDate(dateLst() As String) As String
    Dim tmp1NDT As NotesDateTime        ' 1st comparison
    Dim tmp2NDT As NotesDateTime        ' 2nd comparison
    Dim counter As Long                    ' counter for dateLst
    Dim tmp1date As String                ' temp string for tmp1NDT
    Dim tmp2date As String                ' temp string for tmp1NDT
    tmp1date = ""
    tmp2date = ""
    For counter = 0 To UBound(dateLst())
        tmp2date = dateLst(counter)
        If Not (tmp2date="") Then
            ' compare with first date
            If (tmp1NDT Is Nothing) Then
                ' no 1st date to compare, this date wins by default
                tmp1date = tmp2date
                Set tmp1NDT = New NotesDateTime(tmp1date)
            Else
                ' which date is earlier
                Set tmp2NDT = New NotesDateTime(tmp2date)
                If (tmp2NDT Is Nothing) Then
                    ' skip testing new date, as passed date is invalid, current one if any wins
                Else
                    ' compare dates
                    If (tmp2NDT.Timedifferencedouble(tmp1NDT) < 0) Then
                        ' tmp2NDT is earlier, keep current date
                    Else
                        ' tmp2NDT is later, replace current testing date
                        tmp1date = tmp2date
                        Set tmp1NDT = New NotesDateTime(tmp2date)
                    End If
                End If
            End If
        End If
    Next counter
    ' return latest date
    GetLatestDate = tmp1date
End Function

______________________________________________

@Formula for R6 or Later to Get Latest Date

tmpAllDates:=@ToTime(@Trim( @Text(Fld1) : @Text(Fld2) : @Text(Fld3) : @Text(Fld4) );
@For(n := 1;
n <= @Elements(tmpAllDates);
n := n + 1;
tmpdate:=@If(tmpdate=""; tmpAllDates[n]; tmpdate< tmpAllDates[n]; tmpAllDates[n]; tmpdate)
);
@SetField("Frm_LastDateFld"; tmpdate);


Pseudo @Formula Code for Pre-R6:

Collect all dates into a list, use @date to get date portion and @trim to trim out empty elements
AllDates:=(@TextToTime(@Trim( Date1: Date2: ....:Date9))

TotElements = @elements(AllDates); REM"How many do we have"

IF TotElements = 0 EarliestDate = "" @return

EarliestDate = Subset(AllDates;1)

IF TotElements = 1 @return REM "well there is nothing more to check

if EarliestDate < @subset(AllDates; 2) THEN EarliestDate= @subset(Alldates;2)
if TotElements = 2 ; @return("") 'Check Have we reached the max no. of valid entries

if EarliestDate < @subset(AllDates; 3) THEN EarliestDate= @subset(Alldates; 3)
if TotElements = 3 ; @return("") 'Check Have we reached the max no. of valid entries

Go on with the 2 lines incrementing the numbers till you achieve your max permissible entries..

previous page