Given the current date and birth date, find the present age.
Examples:
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 , 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 is as follows:
OutputPresent Age
Years: 7 Months: 11 Days: 22
Time Complexity: O(1)
Auxiliary Space: O(1)