;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       TimeDiff() 
;; 
;;ACTION         Calculates the time difference between two given date/time strings. 
;;               Returns seconds by default, can return minutes, hours, days, or years. 
;;               Can also return milliseconds part when returning seconds. 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        2.4  - 2019/10/01 
;; 
;;HISTORY        2.0  - 2006/11/20 - Changes for code efficiency; added defaults for midnight  
;;               2.1  - 2007/03/17 - Added "now" and "today" options for both start and end times 
;;               2.2a - 2007/10/17 - Modified to increase accuracy, permit fracional second calculations 
;;               2.3  - 2014/12/24 - Enhanced format returned to restrict result to 3 decimal places 
;;               2.4  - 2019/10/01 - Handle conversion of date format with dashes (yyyy-mm-dd) to slashes 
;; 
;;SYNTAX         TimeDiff(Start [, End] [, Format] [, MSec]) 
;; 
;;PARAMETERS     Start  - REQUIRED - String 
;;               - A value representing the start timestamp 
;;                 Format must be "yyyy/mm/dd hh:mm:ss[.sss]."  
;; 
;;               End - OPTIONAL - String 
;;               - Defaults to "now" 
;;               String value representing the ending time 
;;               Format yyyy/mm/dd hh:mm:ss 
;; 
;;               Both Start and End can be the special value "now" for the current  
;;               date/time, or "today" for midnight of the current day. 
;; 
;;               When the time value is not specified, it defaults to 00:00:00.000 (midnight).  
;;               The default time value as well as the keywords Now and Today include 
;;               milliseconds in the calculation. 
;; 
;;               Format - OPTIONAL, one of: 
;;                "m" - return minutes 
;;                "h" - return hours 
;;                "d" - return days 
;;                "y" - return years 
;;               When a format value is specified, it returns the fractional part 
;;               (ie 0.5 days for 12 hours). 
;; 
;;               MSec	- OPTIONAL, True if the fractional seconds should be returned. Default 
;;               is false, returning whole seconds, to maintain compatibility with earlier versions. 
;;               MSec only affects the return of fractional seconds, not fractional parts of other time formats. 
;; 
;;REMARKS        Returns a value representing the difference in time between two date/time 
;;               strings. Assumes that "Start" is in the past, but will properly return a 
;;               negative value if it is in the future. 
;; 
;;RETURNS        Double - difference between Start and End timestamps in seconds 
;;               or the requested format. 
;; 
;;DEPENDENCIES   None 
;; 
;;TESTED WITH    Kix 4.2+, NT4, W2K, WXP, W2K3, W2K8, W2K12, Win7, Win8 
;; 
;;EXAMPLES       If TimeDiff(GetFileTime('SomeFile.txt'),  'now', 'h') > 48 
;;		   "File is more than 2 days old!" ? 
;;		 EndIf 
; 
Function TimeDiff($_Start, OPTIONAL $_End, OPTIONAL $_Fmt, OPTIONAL $_MSec)
 
  Dim $_						; Temp Var 
  Dim $_SDate, $_EDate					; Start & End Date 
  Dim $a_Start						; Start time array 
  Dim $a_End						; End time array 
  Dim $_Duration					; Difference between start & end 
  Dim $_Now						; allow immediate determination of "now" 
 
  $_Now = @DATE + ' ' + @TIME + '.' + @MSECS
 
  ; Check for special START parameters 
  Select
   Case $_Start = 'now'
    $_Start = $_Now
   Case $_Start = 'today'
    $_Start = @DATE + ' 00:00:00.000'
  EndSelect
 
  ; Check for special END parameters 
  Select
   Case $_End = 'now' Or $_End = '' 
    $_End = $_Now
   Case $_End = 'today'
    $_End = @DATE + ' 00:00:00.000'
  EndSelect
 
  ; Validate parameters 
  ; Parameters passed are "yyyy/mm/dd hh:mm:ss[.sss]" - make sure the default time is added 
  If InStr($_Start, '-') $_Start = Replace($_Start, '-', '/') EndIf
  $a_Start = Split(Join(Split(Join(Split($_Start + ' 00:00:00.000', '/'), ' '), ':'), ' '), ' ', 6)
  If UBound($a_Start) <> 5 Exit 87 EndIf		; bad start time parameter 
  For $_ = 0 to 5
    $a_Start[$_] = CDbl($a_Start[$_])			; convert to numeric values 
  Next
 
  If InStr($_End, '-') $_End  = Replace($_End, '-', '/') EndIf
  $a_End = Split(Join(Split(Join(Split($_End + ' 00:00:00.000', '/'), ' '), ':'), ' '), ' ', 6)
  If UBound($a_End) <> 5 Exit 87 EndIf			; bad start time parameter 
  For $_ = 0 to 5
    $a_End[$_] = CDbl($a_End[$_])			; convert to numeric values 
  Next
 
  ; Convert dates to Days, then convert to seconds and add the time value 
  If $a_Start[1] < 3
    $a_Start[1] = $a_Start[1] + 12
    $a_Start[0] = $a_Start[0] - 1
  EndIf
  $_SDate = $a_Start[2] + ( 153 * $a_Start[1] - 457 ) / 5 + 365 * $a_Start[0] + $a_Start[0] / 4 - $a_Start[0] / 100 + $a_Start[0] / 400 - 306
  $_SDate = CDbl($_SDate) * 86400.0
  $_SDate = $_SDate + $a_Start[3] * 3600 + $a_Start[4] * 60 + $a_Start[5]
 
  If $a_End[1] < 3
    $a_End[1] = $a_End[1] + 12
    $a_End[0] = $a_End[0] - 1
  EndIf
  $_EDate = $a_End[2] + ( 153 * $a_End[1] - 457 ) / 5 + 365 * $a_End[0] + $a_End[0] / 4 - $a_End[0] / 100 + $a_End[0] / 400 - 306
  $_EDate = CDbl($_EDate) * 86400.0
  $_EDate = $_EDate + $a_End[3] * 3600 + $a_End[4] * 60 + $a_End[5]
 
  ; Get the duration between the timestamps 
  $_Duration = CDbl($_EDate - $_SDate)
 
  ; Return data as a Double - seconds (default), hours, minutes, days, or years - restrict to 3 decimal places 
  Select
   Case $_Fmt = 'm'	; minutes 
    $TimeDiff = CDbl(FormatNumber($_Duration / 60.0, 3, 0, 0, 0))
   Case $_Fmt = 'h'	; hours 
    $TimeDiff = CDbl(FormatNumber($_Duration / 3600.0, 3, 0, 0, 0))
   Case $_Fmt = 'd'	; days 
    $TimeDiff = CDbl(FormatNumber($_Duration / 86400.0, 3, 0, 0, 0))
   Case $_Fmt = 'y'	; years 
    $TimeDiff = CDbl(FormatNumber($_Duration / 31536000.0, 3, 0, 0, 0))
   Case 1
    ; Trim fractional seconds if the MSec flag wasn't set - Value returned is whole seconds 
    If $_MSec
      $_Duration = CDbl(FormatNumber($_Duration, 3, 0, 0, 0))		; restrict to 3 decimal points 
    Else
      $_Duration = CDbl(FormatNumber($_Duration, 0, 0, 0, 0))		; return whole number of seconds 
    EndIf
    $TimeDiff = $_Duration
  EndSelect
 
  Exit 0
 
EndFunction