CFLib.org – Common Function Library Project

AgeSinceDOB(dob)

Last updated November 18, 2002

Version: 1 | Requires: ColdFusion 5 | Library: DateLib

 
Rated 2 time(s). Average Rating: 5.0

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:

view plain print about
<cfoutput>ageSinceDOB(1/26/1979)=#ageSinceDOB("1/26/1979")#</cfoutput>

Parameters:

Name Description Required
dob Date of birth. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
16 day(s) ago

James Sleeman James Sleeman added
quickSort
26 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
29 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson