CFLib.org – Common Function Library Project

AgeSinceDOB(dob)

Last updated November 18, 2002

author

Alexander Sicular

Version: 1 | Requires: CF5 | Library: DateLib

Description:
Given the date of birth, returns age. ie: 23y or 11m or 6d. Also see GetCurrentAge, which will only return a strict year result.

Return Values:
Returns a string.

Example:

<cfoutput>ageSinceDOB(1/26/1979)=#ageSinceDOB("1/26/1979")#</cfoutput>

Parameters:

Name Description Required
dob Date of birth. Yes

Full UDF Source:

/**
 * Given the date of birth, returns age.
 * 
 * @param dob      Date of birth. (Required)
 * @return Returns a string. 
 * @author Alexander Sicular (as867@columbia.edu) 
 * @version 1, November 18, 2002 
 */
function ageSinceDOB(dob) {
  
  var ageYR = DateDiff('yyyy', dob, NOW());
  var ageMO = DateDiff('m', dob, NOW());
  var ageWK = DateDiff('ww', dob, NOW());
  var ageDY = DateDiff('d', dob, NOW());
  var age = "";
  
  if ( isDate(dob) ){    
    if (now() LT dob){
      age = "NA";
    }else{  
      if (ageYR LT 2) {
        age = ageMO & "m";
          if (ageMO LT 1) {
            age = ageWK & "w";
          }
          if (ageWK LT 1) {
            age = ageDY & "d";
          }
      }else{
        age = ageYR & "y";
      }  
    }  
  }else{    
    age = "NA";
  }  
  return age;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson