# Calculate age from date of birth in C++

In this tutorial, we will learn how to calculate age from date of birth in C++.

## Calculate age from date of birth in C++

In the following program, we will take as input the present date, present month, present year, birth date, birth month and birth year. All these values are represented by the variables pd, pm, py, bd, bm, and by respectively. We take these values as input in the basic date format of dd mm yyyy. Then, we pass these values to the function age() as parameters. The function then does the task of calculating the age. We find the year y by simply subtracting the values of py and by. We find the month m by subtracting the values of pm and bm if pm>bm. Otherwise, we subtract 1 from y and subtract the quantity (bm – pm) from 12.  Similarly, we find the days d by subtracting the values of bd and pd if pd>bd. Otherwise, we subtract the value (bd – pd) from the days of that month, i.e. the pm. We access the days of the present month from the md array which has values as the number of days in the respective month.

C++ Program :

```#include <iostream>
using namespace std;

void age(int pd, int pm, int py,int bd, int bm, int by)
{
int d, m, y;
int md[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
y = py - by;
if (pm < bm)
{
y--;
m = 12 - (bm - pm);
}
else
{m = pm - bm;}
if (pd < bd)
{
m--;
d = md[pm - 1] - (bd - pd);
}
else
{d = pd - bd;}
cout << "your age is : \n";
cout << y << " years " << m << " months " << d << " days. ";
}
int main()
{
int pd, pm, py, bd, bm, by;
cout << " Enter the present date in the format dd mm yyyy : ";
cin >> pd >> pm >> py;
cout << " Enter the birth date in the format dd mm yyyy : ";
cin >> bd >> bm >> by;
age(pd, pm, py, bd, bm, by);

return 0;
```

Output :

Enter the present date in the format dd mm yyyy : 16 10 2019
Enter the birth date in the format dd mm yyyy : 14 12 1996
22 years 10 months 2 days.

Hope this was helpful. Enjoy Coding!

Also learn :

Date and Time in C++

Namespaces in C++

### 5 responses to “Calculate age from date of birth in C++”

1. Aokp says:

Thank you!

2. purushothji says:

/*The input of date of birth should be in the format of
DD-MM-YYYY
DD/MM/YYYY
01/01/1999
DD:MM:YYYY
*/

#include
#include
#include
#include

using namespace std;

time_t now = time(0);
tm *ltm = localtime(&now);

int currentYear=1900 + ltm->tm_year;
int currentMonth=1 + ltm->tm_mon;
int currentDate=ltm->tm_mday;

class BirthDay{
public:
BirthDay(){
}

string convert(string dateOfBirth){
string date=dateOfBirth.substr(0,2);
int dateToInt=stoi(date);
string month=dateOfBirth.substr(3,5);
int monthToInt=stoi(month);
string year=dateOfBirth.substr(6,10);
int yearToInt=stoi(year);
int mtd=monthToDays(monthToInt,yearToInt);
int daysCalc=mtd+dateToInt;
int ytd=yearToDays(yearToInt);
int TotalDays =currentData();

int diffDays=abs(TotalDays-daysCalc);
int result=ytd+diffDays;
if (TotalDays <daysCalc)
result=ytd-diffDays;

cout<<"Days: "<<endl;
cout<<result<<endl;

cout<<"Hours: "<<endl;
cout<<result*24<<endl;
cout<<"Minutes :"<<endl;
cout<<result*24*60<<endl;
cout<<"Seconds: "<<endl;
cout<<result*24*3600<<endl;
return dateOfBirth ;

}

int monthToDays(int month,int year){

int months={31,28,31,30,31,30,31,31,30,31,30,31};
if(year%4==0)
months=29;
int monToDays=0;
for(int i=0;i<month-1;i++){

monToDays+=months[i];
}

return monToDays;

}

int yearToDays(int year){
int age=currentYear-year;
int daysPerYear=365;
int leap=age/4;
int days=age*daysPerYear+leap;
return days;

}

int currentData(){
int months={31,28,31,30,31,30,31,31,30,31,30,31};
if(currentYear %4==0)
months=29;
int currentDays=0;
for(int i=0;i>dateOfBirth;
BirthDay dateofbirth;
dateofbirth.convert(dateOfBirth);

return 0;
}

3. Hilary says:

Thank you very much. I’d got the program to help make quick calculation of people’s ages and it works just as expected.

4. holyboi says:

I really appreciated this awesome work.I helps me fix some stuff that hooked me