Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Calculating Age in Years from Date of Birth

Calculating Age in Years from Date of Birth

Post by Ankit Arora (forceguru)  (2011-06-04)

Status: Unverified
Level: intermediate

Problem

You want to calculate someone's age in years using a formula field, given a date of birth.

Solution

Here's a formula that will calculate the exact age from the birth date. We assume you've got the date stored in a field DOB__c.

IF(
  MONTH(TODAY())>MONTH(DOB__c),
    YEAR(TODAY())-YEAR(DOB__c),  
    IF(AND(MONTH(TODAY()) = MONTH(DOB__c), DAY(TODAY()) >= DAY(DOB__c)), 
      YEAR(TODAY())-YEAR(DOB__c), 
      (YEAR(TODAY())-YEAR(DOB__c))-1)
)

Description

The formula works by checking several cases:

First: if the month of present year is greater than date of birth, then the age will be calculated as the difference between present year and year of birth.

Secondly: if the months of the two dates are the same, but today's day is larger than or equal to the day of the date of birth, then the age is calculate as in the first scenario.

Thirdly: if both of these scenarios fail, then we have a day of birth before before the present day, in which case the age is calculated as the difference between present year and year of birth, subtracting 1.

Share

Recipe Activity - Please Log in to write a comment

hi

by a093000000WcVBJ  (2013-04-02)

No major critisisms since it obviously works, but it strikes me that there are too many checks - why not check the complete date rather than the month and days individually? The following gives a smaller compile size and is easier to follow. It takes the difference in years, then subtracts 1 if they haven't had a birthday this year yet.

YEAR(TODAY()) - YEAR(DOB__c) -
IF(
DATE(YEAR(TODAY()),MONTH(DOB__c),DAY(DOB__c)) > TODAY(), 1, 0)

by a093000000WcCuG  (2013-02-22)

i want to calulate in total years,months and days

by pushpa tulasi  (2012-07-12)

Good Ankit.
i have implemented this..its looking fine..
 

by test6 test6  (2011-06-20)

Good Ankit.
i have implemented this..its looking fine..

by test6 test6  (2011-06-20)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.