+ Start a Discussion
Balaji B 10Balaji B 10 

how to get the date of birth ,with age years,months and days?

Best Answer chosen by Balaji B 10
Nagendra Prasad ChNagendra Prasad Ch
Here is the code in apex,
 
​date DOB = system.today();

integer days = 1;// Put days of age here
integer months = 1;// Put months of age here
integer years = 1;// Put years of age here


DOB = DOB.addyears(-years).addmonths(-months).adddays(-days);
system.debug('DOB : '+DOB);


 

All Answers

Nagendra Prasad ChNagendra Prasad Ch
Here is the code in apex,
 
​date DOB = system.today();

integer days = 1;// Put days of age here
integer months = 1;// Put months of age here
integer years = 1;// Put years of age here


DOB = DOB.addyears(-years).addmonths(-months).adddays(-days);
system.debug('DOB : '+DOB);


 
This was selected as the best answer
AG_SFDCAG_SFDC
Hi,

You can achieve this using a formula field:

Make sure formula field returns text type and replace "DOB__c" with your custom field.
 
if(And(Month(Today())>= month(DOB__c), Day(Today()) >=  Day(DOB__c)),

TEXT(Year(Today())- Year(DOB__c)) &" "&"years"&" "&
TEXT(Month(Today())-Month(DOB__c)) &" "&"months"&" "&
TEXT(Day(Today())-Day(DOB__c)),

if(And(month(Today())< month(DOB__c), Day(Today())>= Day(DOB__c)),

TEXT(Year(Today())-Year(DOB__c)-1)&" "&"years"&" "&
TEXT(12 - Month(DOB__c)+month(Today()))&" "&"months"&" "&
TEXT(Day(Today())-Day(DOB__c)),

if(And( month(Today())< month(DOB__c), Day(Today())< Day(DOB__c)),

(TEXT( (Year(Today())-Year(DOB__c)-1))&" "&"years"&" "&
TEXT((12 - Month(DOB__c)+month(Today())-1))&" "&"months")&" "&
if( month(DOB__c)= 4, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 6, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 9, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 11,TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 2, TEXT( (28 - Day(DOB__c))+ Day(Today())),
TEXT( (31 - Day(DOB__c))+ Day(Today()))))))),

if(And( month(Today())= month(DOB__c), Day(Today())< Day(DOB__c)),

(TEXT(Year(Today())- Year(DOB__c)-1))&" "&"years"&" "&
"11"&" "&"months"&" "&
if( month(DOB__c)= 4, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 6, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 9, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 11,TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 2, TEXT( (28 - Day(DOB__c))+ Day(Today())),
TEXT( (31 - Day(DOB__c))+ Day(Today()))))))),

(TEXT(Year(Today())- Year(DOB__c)))&" "&"years"&" "&
TEXT(Month(Today())-Month(DOB__c)-1)&" "&"months"&" "&
if( month(DOB__c)= 4, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 6, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 9, TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 11,TEXT( (30 - Day(DOB__c))+ Day(Today())),
if( month(DOB__c)= 2, TEXT( (28 - Day(DOB__c))+ Day(Today())),
TEXT( (31 - Day(DOB__c))+ Day(Today()))))))))

)))&" "&"days"


Let me know if it helped you!

Refer to this for more info: https://success.salesforce.com/answers?id=90630000000gpj3AAA


Thanks,
AG
Balaji B 10Balaji B 10
Thank u Ch Nagendra Prasad,