Program to calculate age
Given current date and birth date, find the present age.
Input : Birth date = 07/09/1996 Present date = 07/12/2017 Output : Present Age = Years: 21 Months: 3 Days: 0 t Age = Years: 7 Months: 11 Days: 21
While calculating the difference in two dates we need to just keep track of two conditions that will do.
- If the current date is less than that of the birth date, then that month is not counted, and for subtracting dates we add number of month days to the current date so as to get the difference in the dates.
- If the current month is less than the birth month, then the current year is not taken into count as this year has not been completed and for getting the difference of months, we subtract by adding 12 to the current month.
- At the end we just need to subtract the days, months and years to get the difference after the two conditions are dealt with.
Below is the implementation of the above approach :
Present Age Years: 7 Months: 11 Days: 22
Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.