[futurebasic] Re: [FB] Days between dates

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2003 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Sat, 10 May 2003 11:58:46 +0200

Craig Hoyt wrote:
> Thanks Ken,
> Also thanks to Michael for his routine. I choose to adapt Kens because it
> was able to cross millennium boundaries and also handled leap years. I took
> the liberty to role Kens example into a single function as my application
> calls it over a dozen times and I didn't want to parse the day,month & year
> twice for every call. It also uses FB's DATE$ as one of parameters in about
> half of them. Below is what I came up with. Pass two dates (in MM/DD/YY
> string format) and it returns the days between. I'm sure it can be more
> elegant by many of you but it works for me and maybe others can find a use
> for it too.
> 
> Thanks again.
> 
> LOCAL FN CountDays(Dt1$,Dt2$)
> DIM  dd1, dd2, mm1, mm2, yy1, yy2
> DIM ds1#, ds2#
> Dim as double y1, y2
> 
> dd1 = VAL(MID$(Dt1$,4,2))'Parse day,month,year
> mm1 = VAL(MID$(Dt1$,1,2))
> yy1 = VALl(MID$(Dt1$,7,2))
> dd2 = VAL(MID$(Dt2$,4,2))
> mm2 = VAL(MID$(Dt2$,1,2))
> yy2 = VAL(MID$(Dt2$,7,2))
> if yy1<50 then yy1=yy1+2000 else yy1=yy1+1900'convert to full year format
> if yy2<50 then yy2=yy2+2000 else yy2=yy2+1900
> y1 = yy1+(mm1-2.85)/12'Kens stuff
> ds1# = fix(fix(fix(367*y1)-1.75*fix(y1)+dd1)-.75*fix(.01*y1))+1721119
> y2 = yy2+(mm2-2.85)/12
> ds2# = fix(fix(fix(367*y2)-1.75*fix(y2)+dd2)-.75*fix(.01*y2))+1721119
> END FN = abs(ds1#-ds2#)
> 
> Craig

I feel a Rosetta time...
I bet you won't see any difference in your program, but you can make 
run the function a tiny bit faster using VAL& instead of VAL, because 
it works here with integers.
Losing the pure BASIC code, here is another possibility:

Local Mode
Dim As Double  y1, y2, ds1, ds2
Dim As Long   dd1, dd2, mm1, mm2, yy1, yy2
Local Fn CountDays(@Dt1 As .Str15, @Dt2 As .Str15)
Dt1.3`` = 2 : Dt1.6`` = 2
Dt2.3`` = 2 : Dt2.6`` = 2
mm1 = Val&(Dt1.0$)
dd1 = Val&(Dt1.3$)
yy1 = Val&(Dt1.6$)
mm2 = Val&(Dt2.0$)
dd2 = Val&(Dt2.3$)
yy2 = Val&(Dt2.6$)
Dt1.3`` = _"/" : Dt1.6`` = _"/" // only if you need to work
Dt2.3`` = _"/" : Dt2.6`` = _"/"// with the date strings afterward
If yy1<50 Then yy1+=2000 Else yy1+=1900
If yy2<50 Then yy2+=2000 Else yy2+=1900
y1 = yy1+(mm1-2.85)/12'Kens stuff
ds1 = Fix(Fix(Fix(367*y1)-1.75*Fix(y1)+dd1)-.75*Fix(.01*y1))+1721119
y2 = yy2+(mm2-2.85)/12
ds2 = Fix(Fix(Fix(367*y2)-1.75*Fix(y2)+dd2)-.75*Fix(.01*y2))+1721119
End Fn = Abs(ds1-ds2)

Alain