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)
    m = 12 - (bm - pm);       
  {m = pm - bm;}
  if (pd < bd)
    d = md[pm - 1] - (bd - pd);
  {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;

Run this code online
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
your age is :
22 years 10 months 2 days.

Hope this was helpful. Enjoy Coding!

Also learn :

Date and Time in C++

Namespaces in C++

7 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


    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{
    cout<<"Welcome\nYour details"<<endl;

    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)

    cout<<"Days: "<<endl;

    cout<<"Hours: "<<endl;
    cout<<"Minutes :"<<endl;
    cout<<"Seconds: "<<endl;
    return dateOfBirth ;


    int monthToDays(int month,int year){

    int months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int monToDays=0;
    for(int i=0;i<month-1;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[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    if(currentYear %4==0)
    int currentDays=0;
    for(int i=0;i>dateOfBirth;
    BirthDay 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

  5. madhawa says:

    I got the program to help make quick calculation of people’s ages but not work leap year

  6. Thad says:

    In playing around with program, it works, but how can you get down to specifics on the days calculation, line 19 I believe. You can incorporate a leap year fix into the program, on line 19. I do not want rough guess’. I would like to get down to specifics please. Is this possible please. Overall the program work well.

  7. Janke Kya Krega says:

    you could have used “struct” also to make the problem more easier…

Leave a Reply

Your email address will not be published. Required fields are marked *