VerbaliseDuration()
Posted: Sun May 22, 2011 2:11 am
This isn't much of a trick or tip, but I thought I'd post it anyway.
You get the difference between two dates, as an integer, and feed that into this procedure. It returns the difference in a verbalised form. For example, "2 years, 5 months and 1 day".
Hope someone can use it.
You get the difference between two dates, as an integer, and feed that into this procedure. It returns the difference in a verbalised form. For example, "2 years, 5 months and 1 day".
Code: Select all
Procedure.i DayZeroTime(datelong.i)
If datelong=0 : ProcedureReturn 0 : EndIf
y = Year(datelong)
m = Month(datelong)
d = Day(datelong)
ProcedureReturn Date(y,m,d,0,0,0)
EndProcedure
Procedure.i DaysSince(begin.i,now.i)
begin = DayZeroTime(begin)
now = DayZeroTime(now)
elapsed.f = now-begin
elapsed = elapsed/24/60/60
ProcedureReturn Int(elapsed)
EndProcedure
Macro EnsureThisNotEnd(t,endd)
If Right(t,Len(endd)) = endd
t = Left(t,Len(t)-Len(endd))
EndIf
EndMacro
Procedure.s VerbaliseDuration(days.i)
While days>365
years+1
days-365
Wend
While days>30
months+1
days-30
Wend
t.s
If years>0
If years=1
t = "1 year|"
Else
t = Str(years)+" years|"
EndIf
EndIf
If months>0
If months=1
t + "1 month|"
Else
t + Str(months)+" months|"
EndIf
EndIf
If days>0
If days=1
t + "1 day|"
Else
t + Str(days)+" days|"
EndIf
EndIf
t2.s = ""
items = CountString(t,"|")
For a = 1 To items
this.s = StringField(t,a,"|")
t2+this
If a<items
If a=(items-1)
t2+" and "
Else
t2+", "
EndIf
EndIf
Next a
ProcedureReturn t2
EndProcedure
birth = Date(1982,11,21,11,25,0)
now = Date()
ds.i = DaysSince(birth,now)
Debug ds
Debug VerbaliseDuration(ds)