After substracting the 2 years and months from the given dates (resulting in something like a 5 line js function), they told me “No, it has to be day-specific, also taking into account leap years!”.
As it turned out, 70.5 actually meant 70 years, 6 months and 0 days. 70 years, 6 months and 1 day was already too old.
Date can tell how many days are in a month (with regards to leap years and 30/31 days).
new Date(2016, 2, 0).getDate(); outputs 29, while
new Date(2015, 2, 0).getDate(); outputs 28.
With the precision requirement in mind, I started thinking of an age as of a json object of format:
and that’s how I implemented a function
which takes the
birthdate date object, calculates and returns the age as a json object of the above format.
After that they said “Ok, looks nice, but we forgot to mention the reference date is the first of next month, not the present date”.
Following this, I refactored the code and came up with
function timePassed(date, refdate) from bellow.
I also wrote
function compareDates(date1, date2) which compares 2 json dates. I used it like this:
Bellow are the 3 functions: timePassed, compareDates and daysInMonth. I used Windows’ Calculator (View -> Date calculation) to test this and found no issues so far. Let me know if you find any bugs and I will correct them.