Sign in to follow this  
Followers 0
orbs

_DateDiffAsApproxString - simple UDF for humane representation of date diff

1 post in this topic

#1 ·  Posted (edited)

this function returns a date diff in a human-readable format. examples - see screenshots (generated randomly by the example script attached):

_DateDiffAsApproxString_-_ex1.thumb.png.

_DateDiffAsApproxString_-_ex2.thumb.png.

_DateDiffAsApproxString_-_ex3.thumb.png.

_DateDiffAsApproxString_-_ex4.thumb.png.

the UDF:

#include-once
#include <Date.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _DateDiffAsApproxString
; Description ...: Returns a date diff in a human-readable format. Example: "2 months and 18 days"
; Syntax ........: _DateDiffAsApproxString($sDateOld, $sDateNew)
; Parameters ....: $sDateOld - The older date to diff.
;                  $sDateNew - The newer date to diff.
; Return values .: Success - Returns a string of approximate date difference.
;                  Failure - Returns an empty string an sets @error to 1.
; Author ........: orbs
; Modified ......:
; Remarks .......: The returned string is composed of only the first two most significant elements of difference.
;                  Both parameters should be specified in the standard AutoIt date format: "YYYY/MM/DD[ HH:MM:SS]"
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _DateDiffAsApproxString($sDateOld, $sDateNew)
    Local $aResult[2][2]
    Local $iResult = 0
    Local $nTemp = 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
        If $iResult > 1 Then ExitLoop
        $nTemp = _DateDiff($aElement[$iElement][0], $sDateOld, $sDateNew)
        Select
            Case $nTemp <= 0
                ContinueLoop
            Case $nTemp = 1
                $aResult[$iResult][0] = 'a'
                If $iElement=3 Then $aResult[$iResult][0]&='n'
                $aResult[$iResult][1] = $aElement[$iElement][1]
            Case Else
                $aResult[$iResult][0] = $nTemp
                $aResult[$iResult][1] = $aElement[$iElement][1] & 's'
        EndSelect
        $iResult += 1
        $sDateOld = _DateAdd($aElement[$iElement][0], $nTemp, $sDateOld)
    Next
    Switch $iResult
        Case 1
            Return $aResult[0][0] & ' ' & $aResult[0][1]
        Case 2
            Return $aResult[0][0] & ' ' & $aResult[0][1] & ' and ' & $aResult[1][0] & ' ' & $aResult[1][1]
        Case Else
            Return SetError(1, 0, '')
    EndSwitch
EndFunc   ;==>_DateDiffAsApproxString

the example script:

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <Date.au3>
#include "_DateDiffAsApproxString.au3"
Global $sDateOld, $sDateNew
While True
    $sDateNew = _NowCalc()
    $sDateOld = _DateAdd('s', -Random(1, 60 * 60 * 24 * 31 * 12, 1), $sDateNew)
    If MsgBox(5, '_DateDiffAsApproxString() Example', 'Older Date:' & @TAB & $sDateOld & @LF & 'Newer Date:' & @TAB & $sDateNew & @LF & @LF & 'Date Difference:' & @TAB & _DateDiffAsApproxString($sDateOld, $sDateNew)) <> 4 Then ExitLoop
WEnd

 

_DateDiffAsApproxString - Example.au3

EDIT: silly me, it should be "an hour" not "a hour". fixed.

_DateDiffAsApproxString.au3

Edited by orbs

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