GMK

Date/Age Calculator

6 posts in this topic

#1 ·  Posted (edited)

Another one of my hobbies is genealogy research. One of the problems I would come across periodically is finding a death record that contained the exact age in years, months, and days, but was missing either a birth date or death date. So I came up with this UDF of sorts to calculate either the start date (given the end date and age), the end date (given the start date and age), or the age (given the start and end dates).

#include <Date.au3>

; #FUNCTION# ==================================================================
; Name...........: _AgeCalc
; Description ...: Returns an array containing the number of years, months, and
;                  days between a start date and end date.
; Syntax.........: __AgeCalc($sStartDate, $sEndDate)
; Parameters ....: $sStartDate = Start Date (Format: YYYY/MM/DD)
;                  $sEndDate = End Date (Format: YYYY/MM/DD)
; Return values .: Success - An array containing age in years, months, days:
;                            $aArray[0][0] = Years
;                            $aArray[0][1] = "year(s)"
;                            $aArray[1][0] = Months
;                            $aArray[1][1] = "month(s)"
;                            $aArray[2][0] = Days
;                            $aArray[2][1] = "day(s)"
;                  Failure - A NULL string and sets @ERROR:
;                            @error = 1 Start date is invalid
;                            @error = 2 End date is invalid
;                            @error = 3 End date is not greater than start date
; Author ........: GMK
; Related .......: _StartDateCalc, _EndDateCalc
; =============================================================================
Func _AgeCalc($sStartDate, $sEndDate)
    Select
        Case Not _DateIsValid($sStartDate)
            ConsoleWrite("_AgeCalc ==> Start date is invalid." & @CRLF)
            SetError(1)
            Return ""
            Exit
        Case Not _DateIsValid($sEndDate)
            ConsoleWrite("_AgeCalc ==> End date is invalid." & @CRLF)
            SetError(2)
            Return ""
            Exit
        Case Not $sEndDate > $sStartDate
            ConsoleWrite("_AgeCalc ==> End date must be greater than start date." & @CRLF)
            SetError(3)
            Return ""
            Exit
    EndSelect
    Local $aDateArray[3][2]
    $aDateArray[0][0] = _DateDiff("Y", $sStartDate, $sEndDate)
    $aDateArray[0][1] = "year"
    If $aDateArray[0][0] > 1 Then $aDateArray[0][1] &="s"
    $sAnniversary = _DateAdd("Y", $aDateArray[0][0], $sStartDate)
    $aDateArray[1][0] = _DateDiff("M", $sAnniversary, $sEndDate)
    $aDateArray[1][1] = "month"
    If $aDateArray[1][0] > 1 Then $aDateArray[1][1] &="s"
    $sTempDate = _DateAdd("M", $aDateArray[1][0], $sAnniversary)
    $aDateArray[2][0] = _DateDiff("D", $sTempDate, $sEndDate)
    $aDateArray[2][1] = "day"
    If $aDateArray[2][0] > 1 Then $aDateArray[2][1] &="s"
    Return $aDateArray
EndFunc   ;==>_AgeCalc

; #FUNCTION# ==================================================================
; Name...........: _StartDateCalc
; Description ...: Returns the start date calculated backward from an end
;                  date, given years, months and days to subtract.
; Syntax.........: _StartDateCalc($sEndDate, $iAgeYears, $iAgeMonths, $iAgeDays)
; Parameters ....: $iEndDate   - End Date (Format: YYYY/MM/DD)
;                  $iAgeYears  - Number of years as integer
;                  $iAgeMonths - Number of months as integer
;                  $iAgeDays   - Number of days as integer
; Return values .: Success - Start date (in YYYY/MM/DD format)
;                  Failure - A NULL string and sets @ERROR:
;                            @error = 1 End date is invalid
;                            @error = 2 Years must be number
;                            @error = 3 Months must be number
;                            @error = 4 Days must be number
; Author ........: GMK
; Related .......: _AgeCalc, _EndDateCalc
; =============================================================================
Func _StartDateCalc($sEndDate, $iAgeYears, $iAgeMonths, $iAgeDays)
    Select
        Case Not _DateIsValid($sEndDate)
            ConsoleWrite("_StartDateCalc ==> End date is invalid." & @CRLF)
            SetError(1)
            Return ""
            Exit
        Case Not IsNumber($iAgeYears)
            ConsoleWrite("_StartDateCalc ==> $iAgeYears must be a numerical value." & @CRLF)
            SetError(2)
            Return ""
            Exit
        Case Not IsNumber($iAgeMonths)
            ConsoleWrite("_StartDateCalc ==> $iAgeMonths must be a numerical value." & @CRLF)
            SetError(3)
            Return ""
            Exit
        Case Not IsNumber($iAgeDays)
            ConsoleWrite("_StartDateCalc ==> $iAgeDays must be a numerical value." & @CRLF)
            SetError(4)
            Return ""
            Exit
    EndSelect
    $sStartDate = _DateAdd("D", 0 - $iAgeDays, $sEndDate)       ;Subtract days
    $sStartDate = _DateAdd("M", 0 - $iAgeMonths, $sStartDate)   ;Subtract months
    $sStartDate = _DateAdd("Y", 0 - $iAgeYears, $sStartDate)    ;Subtract years
    Return $sStartDate
EndFunc   ;==>_StartDateCalc

; #FUNCTION# ==================================================================
; Name...........: _EndDateCalc
; Description ...: Returns the end date calculated forward from a beginning
;                  date, given years, months and days to add.
; Syntax.........: _EndDateCalc($sStartDate, $iAgeYears, $iAgeMonths, $iAgeDays)
; Parameters ....: $iStartDate - Start Date (Format: YYYY/MM/DD)
;                  $iAgeYears  - Number of years as integer
;                  $iAgeMonths - Number of months as integer
;                  $iAgeDays   - Number of days as integer
; Return values .: Success - End date (in YYYY/MM/DD format)
;                  Failure - A NULL string and sets @ERROR:
;                            @error = 1 End date is invalid
;                            @error = 2 Years must be number
;                            @error = 3 Months must be number
;                            @error = 4 Days must be number
; Author ........: GMK
; Related .......: _AgeCalc, _StartDateCalc
; =============================================================================
Func _EndDateCalc($sStartDate, $iAgeYears, $iAgeMonths, $iAgeDays)
    Select
        Case Not _DateIsValid($sStartDate)
            ConsoleWrite("_EndDateCalc ==> Start date is invalid." & @CRLF)
            SetError(1)
            Return ""
            Exit
        Case Not IsNumber($iAgeYears)
            ConsoleWrite("_EndDateCalc ==> $iAgeYears must be a numerical value." & @CRLF)
            SetError(2)
            Return ""
            Exit
        Case Not IsNumber($iAgeMonths)
            ConsoleWrite("_EndDateCalc ==> $iAgeMonths must be a numerical value." & @CRLF)
            SetError(3)
            Return ""
            Exit
        Case Not IsNumber($iAgeDays)
            ConsoleWrite("_EndDateCalc ==> $iAgeDays must be a numerical value." & @CRLF)
            SetError(4)
            Return ""
            Exit
    EndSelect
    $sEndDate = _DateAdd("D", $iAgeDays, $sStartDate)           ;Add days
    $sEndDate = _DateAdd("M", $iAgeMonths, $sEndDate)           ;Add months
    $sEndDate = _DateAdd("Y", $iAgeYears, $sEndDate)            ;Add years
    Return $sEndDate
EndFunc   ;==>_EndDateCalc

Edit: Forgot @CRLF at end of each ConsoleWrite.

Edited by GMK

Share this post


Link to post
Share on other sites



Nice format! Will come in handy in one my scripts

Share this post


Link to post
Share on other sites

I'm trying to get _AgeCalc to work but the $aDateArray returns empty. Here's what I'm doing...

Local $aDateArray[3][2]
Local $sStartDate = "1970/01/01"
Local $sEndDate = "2016/06/28"
_AgeCalc($sStartDate, $sEndDate)
_ArrayDisplay($aDateArray)

What am I doing wrong?

Share this post


Link to post
Share on other sites

Assign the return of the function to the array!

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thank-you, works :)

Share this post


Link to post
Share on other sites

Short version

#include <Date.au3>
#include <Array.au3>

Local $sStartDate = "1970/01/01"
Local $sEndDate = "2016/06/28"
Local $aDateArray = _AgeCalc2($sStartDate, $sEndDate)
_ArrayDisplay($aDateArray)

Func _AgeCalc2($sStartDate, $sEndDate)
    If Not _DateIsValid($sStartDate) Or Not _DateIsValid($sEndDate) Then Return SetError(1, 0, 0)
    Local $aUnit[3] = ["Y", "M", "D"]
    Local $aType[3] = ["Years", "Months", "Days"]
    Local $aReturn[3][2], $iUnit
    For $i = 0 To UBound($aUnit) - 1
        $iUnit = _DateDiff($aUnit[$i], $sStartDate, $sEndDate)
        If $iUnit <> 0 Then
            $aReturn[$i][0] &= $iUnit
            $aReturn[$i][1] &= $aType[$i]
        EndIf
        $sStartDate = _DateAdd($aUnit[$i], $iUnit, $sStartDate)
    Next
    Return $aReturn
EndFunc   ;==>_AgeCalc2

 


Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

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