DTSUB() : Calculates age from two DateTime stamps and returns yyyy/MM/dd HH:mm:ss

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

DTSUB() : Calculates age from two DateTime stamps and returns yyyy/MM/dd HH:mm:ss

22 Oct 2018, 11:54

DTSUB( T1, T2, F )

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 :)
My Scripts and Functions: V1  V2
TAC109
Posts: 1118
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: DTSUB() : Calculates age from two DateTime stamps and returns yyyy/MM/dd HH:mm:ss

22 Oct 2018, 17:29

Very interesting that your function allows for early years, even negative.

However I don’t see a Country parameter, which is needed to allow for the changeover from the Julian calendar in the 16th to 20th centuries, depending on country. Also, is there allowance for such things as the year of confusion (46 BC which had 15 months and 445 days)?
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 115 guests