KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to return an age string between two dates
PRODUCT: 4D | VERSION: 13.3 | PLATFORM: Mac & Win
Published On: August 2, 2013

Ever need to express the age between two dates? Getting the difference in years is easy, see Calculating an Age in Years, getting the age down to the number of days is a little more challenging.

The utility method below will return a string expressing the age between two dates in years, months, and days. It properly accounts for leap years.

If (True)
   If (False)
      Begin SQL
         /*
          Name: UTIL_Days2Age
          Path: UTIL_Days2Age

          Written by Charles Vass - 07/25/2013, 10:27

          Purpose: Utility to express Years, Months and Days between two dates

          $0 - TEXT - Returned Year, Months and Days text expression ($Age)
          $1 - DATE - Earliest date ($OldDate)
          $2 - DATE - Latest date ($YngDate)
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
   C_TEXT($0;$Age)
   C_DATE($OldDate;$1)
   C_DATE($YngDate;$2)
    //--------------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L)
   C_DATE($TmpDate)

End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
$OldDate:=$1
$YngDate:=$2

// Make sure the dates are in the correct order
//
If ($OldDate>$YngDate)
   $TmpDate:=$OldDate
   $OldDate:=$YngDate
   $YngDate:=$TmpDate
End if

// Get the raw differences between the dates
//
$Yrs:=Year of($YngDate)-Year of($OldDate)
$Mons:=Month of($YngDate)-Month of($OldDate)
$Days:=Day of($YngDate)-Day of($OldDate)

//======================== Method Actions ==================================

If ($Days<0) // Old day of month later in the month
     $Mons:=$Mons-1
     $Mo:=Month of($YngDate)-1
     If ($Mo=0)
       $Mo:=12
     End if
     Case of
       : (($Mo=1) | ($Mo=3) | ($Mo=5) | ($Mo=7) | ($Mo=8) | ($Mo=10) | ($Mo=12))
         $Days:=$Days+31

       : ($Mo=2)
         $Days:=$Days+28
         $Yr:=Year of($YngDate)
         If ($Yr%100=0) // May or may not be a leap year
            If ($Yr%400=0) // Is a leap year
               $Days:=$Days+1
            End if
         Else
            If ($Yr%4=0) // Is a leap year
               $Days:=$Days+1
            End if
         End if

       Else
         $Days:=$Days+30

     End case
End if

If ($Mons<0) // Old month later in the year
     $Yrs:=$Yrs-1
     $Mons:=$Mons+12
End if

$Age:=String($Yrs)+" Years"
If ($Mons>0)
     $Age:=$Age+", "+String($Mons)+" Months"
End if
If ($Days>0)
     $Age:=$Age+", "+String($Days)+" Days"
End if

//======================== Clean up and Exit =================================

$0:=$Age