Since 1978 I always used this Function. It was used in the TI-58 TI-59 math module from Texas Instruments. The Formula was made public in their Reference Manual and I ported it to every computer language I ever used.
Code: Select all
Procedure.s NumFormat(Num.q, Fill.s=" ")
form.s=Str(Num)
sl=3
While StringByteLength(form, #PB_Ascii)>sl
form=Left(form, StringByteLength(form, #PB_Ascii)-sl)+Fill+Right(form, sl)
sl+4
Wend
ProcedureReturn form
EndProcedure
Procedure.d GetFaktorFromYMD(yyyy, mm, dd, hour=0, min=0, sec=0, Mode.s="d")
y.d=yyyy
m.d=mm
t.d=dd
h.d=hour
i.d=min
s.d=sec
If m<3
Faktor.d = 365.0 * y + t + 31 * (m - 1.0) + Round((y - 1.0) / 4.0, #PB_Round_Down) - Round(0.75 * (Round(((y - 1.0) / 100.0) + 1.0, #PB_Round_Down)), #PB_Round_Down)
Else
Faktor.d = 365.0 * y + t + 31 * (m - 1.0) - Round(0.4 * m + 2.3, #PB_Round_Down) + Round(y / 4, #PB_Round_Down) - Round(0.75 * (Round(y / 100, #PB_Round_Down) + 1), #PB_Round_Down)
EndIf
Mode=LCase(Mode)
If Mode.s="d"
ProcedureReturn Faktor
ElseIf Mode.s="h"
ProcedureReturn Faktor*24+h
ElseIf Mode.s="m"
ProcedureReturn Faktor*1440+h*60+i
ElseIf Mode.s="s"
ProcedureReturn Faktor*86400+h*3600+i*60+s
EndIf
EndProcedure
f1.q=GetFaktorFromYMD(1969,7,21,3,56)
f2.q=GetFaktorFromYMD(Year(Date()),Month(Date()),Day(Date()),Hour(Date()),Minute(Date()),Second(Date()))
Debug f1
Debug f2
Debug NumFormat(f2-f1, ".") + " days ago the first man put his foot on the moon"
Debug "---"
f1.q=GetFaktorFromYMD(1969,7,21,3,56,0, "h")
f2.q=GetFaktorFromYMD(Year(Date()),Month(Date()),Day(Date()),Hour(Date()),Minute(Date()),Second(Date()), "h")
Debug f1
Debug f2
Debug NumFormat(f2-f1, ".") + " hours ago the first man put his foot on the moon"
Debug "---"
f1.q=GetFaktorFromYMD(1969,7,21,3,56,0, "m")
f2.q=GetFaktorFromYMD(Year(Date()),Month(Date()),Day(Date()),Hour(Date()),Minute(Date()),Second(Date()), "m")
Debug f1
Debug f2
Debug NumFormat(f2-f1, ".") + " minutes ago the first man put his foot on the moon"
Debug "---"
f1.q=GetFaktorFromYMD(1969,7,21,3,56,0, "s")
f2.q=GetFaktorFromYMD(Year(Date()),Month(Date()),Day(Date()),Hour(Date()),Minute(Date()),Second(Date()), "s")
Debug f1
Debug f2
Debug NumFormat(f2-f1, ".") + " seconds ago the first man put his foot on the moon"
Debug "---"