I have a date of birth for each user in my application. E.g. the format is 29/01/2015
My code at the moment calculates the years from today's date and the provided date of birth:
((new Date((new Date().getTime() - new Date(user.dob).getTime())).getFullYear()) - 1970)
For the example above this will retrieve the number 3
since it has been three years since 29/01/2015
.
What would be a good optimization for getting the months if the year is below 1
?
For example for this date: 21/03/2018
, my code will return 0 since a full year has not passed yet. How could I optimize my logic to retrieve 6 months
which is the time between today(21/09/2018
) and 21/03/2018
?
P.S. I would like to avoid the use of any libraries.