The function accepts two DateTime stamps, calculates the difference and returns result formatted as Years,Months,Days,Hours,Minutes,Seconds.
The acceptable range for DateTime stamps are from -9990101000000 to 99991231235959 ( 1000 BC + 1s to AD 10000 - 1s ). Please refer anno Domini before using negative years. (There is no year 0000)
Pass begin date as T1 and end date as T2. Make sure the order isn't reversed. To be clear, when TimeStamps are resolved to the their full form of 14 chars, the parameter T2 must be numerically greater than T1.
About F parameter: The default value is True and returns result formatted as yyyy/MM/dd HH:mm:ss for sorting purpose. (Refer help doc: Sort).
Pass false as 3rd parameter to return the result as an array or pass a string for custom formatting. Refer examples and help doc: Format
The function
Code: Select all
DTSUB( T1, T2, F:=1 ) { ; v2.03 By SKAN | CD:10-Oct-2018 | Topic: goo.gl/L7frVp
Static D := [31,31,0,31,30,31,30,31,31,30,31,30]
Static S := [[1,4],[5,2],[7,2],[9,2],[11,2],[13,2]]
Static U := [0,12,0,24,60,60]
Local R := [0,1,1,0,0,0,0], M,Y,I,xT1,xT2, Adj:=0
M := ( M:=SubStr(T2,5,2)+0 ) ? M : 1
, Y := ( M<>3 ? 0 : SubStr(T2,1,4)+0 )
, U[3] := ( M<>3 ? D[M] : (!Mod(Y,4) && Mod(Y,100) || !Mod(Y,400)) ? 29 : 28 )
While ( I := 7-A_Index )
xT1 := ( xT1:=SubStr(T1, S[I].1, S[I].2)+0 ) ? xT1 : R[I]
, xT2 := ( xT2:=SubStr(T2, S[I].1, S[I].2)+0 ) ? xT2 : R[I]
, R[I] := ( (xT2+=Adj) >= xT1 ? xT2-xT1+(Adj:=0) : xT2+U[I]-xT1+(Adj:=-1)+1 )
R[3] = -1 ? ( R[3]:=30, R[2]:=11, R[1]-=1 ) : 0
Return F ? Format( F=1 ? "{:04}/{:02}/{:02} {:02}:{:02}:{:02}" : F, R* ) : R
}
DTSUB-Examples.ahk
Simple GUI for testing DTSUB()
Thanks