Jump to content

_DateDiffAsApproxString - simple UDF for humane representation of date diff


orbs
 Share

Recommended Posts

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...