Display the difference between two date/time values

Returns the difference in a clean text readable format. Includes both a LotusScript and formula version.
Lotus Formula • Lotus Notes View • LotusScript

LOTUSSCRIPT VERSION - Displays the most appropriate text string based on the date/time difference. For Example : "2 days ago", "1 year ago", "3 months ago", "less than a minute ago"

Dim TimeVar1 As New NotesDateTime( doc.DatePosted(0) )
Dim TimeVar2 As New NotesDateTime( Now )	
	
Dim DiffSecs As Long
Dim DiffMins As Long
Dim DiffHours As Long
Dim DiffDays As Long
Dim DiffWeeks As Long
Dim DiffMonths As Long
Dim DiffYears As Long
	
Dim timestamp As String
	
'**************** TIMEGAP CALC **************
DiffSecs = TimeVar2.TimeDifference( TimeVar1 )
DiffMins = Round((DiffSecs) /60 , 0) 
DiffHours = Round((DiffMins) /60 , 0) 
DiffDays = Round((DiffHours) /24 , 0) 
DiffWeeks = Round((DiffDays) /7 , 0) 
DiffMonths = Round((DiffWeeks) /4 , 0) 
DiffYears = Round((DiffWeeks) /52 , 0) 
'*************************************************
If DiffYears=1 Then									'years
	timestamp = "1 year"
Elseif DiffYears>1 Then
	timestamp = Cstr(DiffYears) & " years"
Elseif DiffMonths=1 Then								'months
	timestamp = "1 month"
Elseif DiffMonths>1 Then
	timestamp = Cstr(DiffMonths) & " months"
Elseif DiffWeeks=1 Then								'weeks
	timestamp = "1 week"
Elseif DiffWeeks>1 Then
	timestamp = Cstr(DiffWeeks) & " weeks"
Elseif DiffDays=1 Then								'days
	timestamp = "1 day"
Elseif DiffDays>1 Then
	timestamp = Cstr(DiffDays) & " days"
Elseif DiffHours=1 Then								'hours
	timestamp = "1 hour"
Elseif DiffHours>1 Then
	timestamp = Cstr(DiffHours) & " hours"
Elseif DiffMins=1 Then								'minutes
	timestamp = "1 minute"
Elseif DiffMins>1 Then
	timestamp = Cstr(DiffMins) & " minutes"				'less then a min
Else
	timestamp = "less then a minute"
End If
'*************************************************
Print timestamp & " ago"

FORMULA VERSION

tmp1 := @Subset(q_NoticeMailed;-1);
tmp2 := q_responsedate ;

tmp := -(@If(@Text(tmp1)!="" & @Text(tmp2)!="" ; tmp1-tmp2 ; 0));
TotalMins := @Integer((tmp/60)) ;
TotalHours := @Integer(TotalMins/60);
DaysDiff := @Integer(TotalHours/24);
HoursDiff := TotalHours-(DaysDiff*24);
MinsDiff := TotalMins-((DaysDiff*1440)+(HoursDiff*60));

@if(
tmp=0 ; "" ;
@Text(DaysDiff) + " days " + 
@Text(HoursDiff) + " hours " + 
@Text(MinsDiff) + " mins"
)

Images/Screenshots:

Posted by fbrefere001 on Friday June 2, 2006