Function DateDiff(Interval As String,dt1 As String,dt2 As String) As Integer 'return the number of whole intervals between two dates 'Result is +ve when DT1<DT2 'DateDiff ("dd","01/02/2001",Now()) returns the difference in whole days since 1st Feb 2001 Local Integer n,s,t,u,v,x,y,z Select Case LCase$(Interval) Case "s"' Seconds DateDiff=UnixTime(dt2)-UnixTime(dt1) Case "m"' Minutes DateDiff=(UnixTime(dt2)-UnixTime(dt1))\60 Case "h"' Hours DateDiff=(UnixTime(dt2)-UnixTime(dt1))\3600 Case "dd" 'Days DateDiff=(UnixTime(dt2)-UnixTime(dt1))\86400 Case "w" 'Weeks DateDiff=(UnixTime(dt2)-UnixTime(dt1))\604800 Case "mm"' calendar Months t=UnixTime(dt1):u=UnixTime(dt2):s=Sgn(u-t) If s Then dt1=HumanTime(Min(t,u)):dt2=HumanTime(Max(t,u)) t=Val(Left$(dt1,2)):u=Val(Mid$(dt1,4,2)):v=Val(Mid$(dt1,7,4)) x=Val(Left$(dt2,2)):y=Val(Mid$(dt2,4,2)):z=Val(Mid$(dt2,7,4)) for n=v+1 to z DateDiff=DateDiff+12 next DateDiff=DateDiff+(y-u) If t>x then DateDiff=DateDiff-1 EndIf DateDiff=DateDiff*s Case "yyyy"' Years t=UnixTime(dt1):u=UnixTime(dt2):s=Sgn(u-t) If s Then dt1=HumanTime(Min(t,u)):dt2=HumanTime(Max(t,u)) t=Val(Left$(dt1,2)):u=Val(Mid$(dt1,4,2)):v=Val(Mid$(dt1,7,4)) x=Val(Left$(dt2,2)):y=Val(Mid$(dt2,4,2)):z=Val(Mid$(dt2,7,4)) DateDiff=z-v If u>y Then DateDiff=DateDiff-1 ElseIf u=y Then If t>x Then DateDiff=DateDiff-1 EndIf EndIf DateDiff=DateDiff*s Case Else DateDiff=0 End Select End Function
Print datediff("dd","01-01-1970 00:00:00","18-01-2018 00:00:00") mod 7 ' should be zero coz both dates are Thursdays
Print (4+datediff("dd","01-01-1970 00:00:00","01-01-1970 00:00:00")) mod 7 ' test forUT0 = 4 (thursday)
Print (4+datediff("dd","01-01-1970 00:00:00",Now())) mod 7' day today
Function DoW(d$) As Integer DoW=(4+DateDiff("dd","01-01-1970 00:00:00",d$)) mod 7 End Function