Sign in to follow this  
Followers 0
orbs

_DateDuration

1 post in this topic

function that returns a literal description of the duration (or date difference) between given dates. UDF and example:

; UDF

#include <Date.au3>
; #FUNCTION# ====================================================================================================================
; Name ..........: _DateDuration
; Description ...: Returns a literal description of the duration (or date difference) between given dates.
; Syntax ........: _DateDuration($sDateOld, $sDateNew[, $iApproxLevel = 6])
; Parameters ....: $sDateOld     - The older date.
;                  $sDateNew     - The newer date.
;                  $iApproxLevel - [optional] amount of elements to return. Default is 6 (all elements).
; Return values .: Success - Returns a string representing the difference between the given dates.
;                  Failure - Returns an empty string and sets @error to 1 if dates are identical or reversed.
; Author ........: orbs
; Modified ......:
; Remarks .......: Dates should be formatted by the AutoIt calculable date format: "YYYY/MM/DD hh:nn:ss" or "YYYY/MM/DD".
;                  The returned string is in English, and formatted literally. Examples:
;                  "2 years, 3 months, 14 days, 20 hours, 41 minutes and 16 seconds" (all elements present)
;                  "2 years, 24 days, 15 hours, 37 minutes and 33 seconds" (no months difference)
;                  "9 days, 5 hours, 42 minutes and 37 seconds" (no years or months differences)
;                  "4 months and 26 days" (there may be hours, minutes or seconds differernces, but parameter $iApproxLevel=2)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _DateDuration($sDateOld, $sDateNew, $iApproxLevel = 6)
    Local $sResult = ''
    Local $aResult[0][2]
    Local $iResult = 0
    Local $iDiffCurr = 0
    Local $aElement[6][2] = [['Y', 'year'], ['M', 'month'], ['D', 'day'], ['h', 'hour'], ['n', 'minute'], ['s', 'second']]
    Local $iElement = 0
    For $iElement = 0 To 5
        $iDiffCurr = _DateDiff($aElement[$iElement][0], $sDateOld, $sDateNew)
        If $iDiffCurr <= 0 Then
            ContinueLoop
        Else
            ReDim $aResult[UBound($aResult) + 1][2]
            $aResult[$iResult][0] = $iDiffCurr
            $aResult[$iResult][1] = $aElement[$iElement][1]
            If $iDiffCurr > 1 Then $aResult[$iResult][1] &= 's'
        EndIf
        $iResult += 1
        $sDateOld = _DateAdd($aElement[$iElement][0], $iDiffCurr, $sDateOld)
    Next
    Local $iReturnElementCount = ($iApproxLevel < UBound($aResult) ? $iApproxLevel : UBound($aResult))
    If $iReturnElementCount = 0 Then Return SetError(1, '', '') ; identical dates or $sDateOld is newer than sDateNew
    If $iReturnElementCount = 1 Then Return $aResult[0][0] & ' ' & $aResult[0][1] ; only one element to return, no need for an elaborate string composition
    For $iResult = 0 To $iReturnElementCount - 2
        $sResult &= ($aResult[$iResult][0] & ' ' & $aResult[$iResult][1] & ', ')
    Next
    $sResult = StringTrimRight($sResult, 2) ; remove last comma and whitespace
    $sResult &= (' and ' & $aResult[$iResult][0] & ' ' & $aResult[$iResult][1])
    Return $sResult
EndFunc   ;==>_DateDuration



; Example

Global $sDateOld = _NowCalc()
Global $sDateNew = _DateAdd('s', Random(0, 100000000, 1), $sDateOld)
Global $iApproxLevel = 3 ; change this in range 1 to 6
Global $sDateDiff = _DateDuration($sDateOld, $sDateNew, $iApproxLevel)
ConsoleWrite('old date = ' & $sDateOld & @CRLF)
ConsoleWrite('new date = ' & $sDateNew & @CRLF)
ConsoleWrite('duration = ' & $sDateDiff & @CRLF)

 

example output:

old date = 2016/12/21 23:21:45
new date = 2017/06/09 23:22:00
duration = 5 months, 19 days and 15 seconds

the text of last line ("5 months, 19 days and 15 seconds") is the return value of the function.

enjoy!

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0