AgeCalculation ( )

Function stats

Average user rating
281
302
9999
Support
FileMaker 7.0 +
Date posted
12 April 2011
Last updated
13 April 2011
Version
Recursive function
No

Author Info
 Marcelo Piñeyro

3 functions

Average Rating None

author_avatar



 

Function overview

Prototype

AgeCalculation  ( dateOfBirth;   dateToCompare )


Parameters

dateOfBirth  


dateToCompare  


Description

Tags:  Date   Age  

Calculates how old a person or a thing was, is, or will be on a user-defined date, based on the date of birth of the person or thing.

Examples

Sample input

AgeCalculation( Date( 4 ; 12 ; 1980 ) ; "" )


Sample output

31 (when the current date is April 12, 2011 for example)

 

Function code

/*
Name:
AgeCalculation

History:
Created by Marcelo Piñeyro, Wingra Consulting LLC, marcelo@wingraconsulting.com
Date created: December 2010
Date modified: April 2011

Purpose:
To calculate how old a person or a thing was, is, or will be on a user-defined date, based on the date of birth of the person or thing.

Parameters:
dateOfBirth: self explanatory. Type: Date.
dateToCompare: the date on which you want to know how old the person or thing was, is, or will be. If left empty it defaults to the current date. Type: Date.

Dependencies:
No dependencies

Example:
AgeCalculation( Date( 4 ; 12 ; 1980 ) ; "" ) returns 31 when the current date is April 12, 2011 for example.

Notes:
The returned value is an integer that represents the number of full years of age.
*/

Let( [
    
    today = If( IsEmpty( dateToCompare ) ; Get( CurrentDate ) ; dateToCompare ) ;
    currentYear = Year( today ) ;
    dateOfBirthMonth = Month( dateOfBirth ) ;
    dateOfBirthDay = Day( dateOfBirth ) ;
    dateOfBirthYear = Year( dateOfBirth ) ;
    bDayThisYear = Date( dateOfBirthMonth ; dateOfBirthDay ; currentYear )
] ;
Case(
    IsEmpty( dateOfBirth ) ; "" ;
    today ≥ bDayThisYear ; currentYear - dateOfBirthYear ;
    currentYear - dateOfBirthYear - 1
)
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/302

    Prototype: AgeCalculation( dateOfBirth; dateToCompare )
    Function Author: Marcelo Piñeyro (http://www.fmfunctions.com/mid/281)
    Last updated: 13 April 2011
    Version: 2.0

*/
// ===================================

 

Comments

Fabrice
12 April 2011



This one looks very promising.
If I can suggest something :
- why 3 parameters to pass a date ? All 3 are mandatory, so you could have a simple Date parameter.
- on the contrary, you could add one for the reference date, instead of using the current date. It is often interesting to know what age will someone be at a given (future) date.
  General comment
Marcelo Piñeyro
13 April 2011



Fabrice, thank you for your comments. You are right regarding using 3 parameters to pass a date. I did it that way because in the solution where I originally created the custom function, my client wanted to have a field for day, a field for month, and a field for year, with their respective value lists, but really all that is needed to pass the date of birth into the function is 1 parameter.

I also liked your suggestion to make the function more flexible in terms of being able to determine the age on any given date, not just on the current date. I did think about that before posting the function, but I wasn't sure that anyone would care about it. Your comment encouraged me to implement this feature!

I have uploaded a new version of the function that incorporates both things discussed above.

Marcelo
  General comment

 

 

 

 

 

Top Tags

Text Parsing  (31)
Date  (26)
List  (25)
Format  (23)
XML  (22)
Sql  (18)
Dev  (17)
Debug  (14)
Text  (13)
Variables  (12)
Interface  (12)
Layout  (11)
Filter  (10)
Design  (9)
Array  (7)
Layout Objects  (7)