Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 2 years ago

Age Calculator using HTML, Tailwind and JS Form Validation

tailwind-css
Daniel Aadland•100
@GreenCitrus6
A solution to the Age calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


I struggled with getting the calculated age to be consistent across different lengths of time. The approach I took was to:

  1. Fetch the current time as a millisecond time stamp
  2. Convert the user inputted date to a millisecond time stamp
  3. Find the difference between the two time stamps
  4. Convert that difference into a number of years by dividing by the average number of milliseconds in a year
  5. Convert the remainder after calculating years into months by dividing by the average number of milliseconds in a month
  6. Convert the remainder after calculating months into days by dividing by the number of milliseconds in a day
  7. Use Math.floor() to round years, months and days down

The code for the calculation is as follows:

 function timestampToYMD(timestampDiff) {
        // calculated age is not accurate, try converting from ms to days first, then to months and years?
        const MsInADay = (1000 * 60 * 60 * 24)
        const NumOfLeapYears = (1000/4) - 7;
        const ExactMonthLength = 
        // Average length of a month, accounting for leap years using the number of leap years between 1000 and 2000
        (((1000 - ((1000/4) - 7)) * ((31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31) / 12) //average month length in a normal year
        + 
        (((1000/4) - 7) * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31) / 12) ) //average month length in a leap year
         / 1000 /* averaging out the normal year month length and leap year month length over the span of 1000 years */);
        const MsInAYear = (
            // taking years of 1001 to 2000
            /* LEAP YEAR RULES: 
                Divisible by 4
                For centuries, those divisible by 400
            */
            (365 + (NumOfLeapYears/ 999)) * MsInADay
        );
        // 31_557_016_216.21622
        let numOfYears = timestampDiff / MsInAYear;
        let numOfMonths = ((timestampDiff % MsInAYear) / (MsInADay * ExactMonthLength));
        let numOfDays = ((timestampDiff % MsInAYear) % (ExactMonthLength * MsInADay) / MsInADay);

        
        document.querySelector("#num-of-years").innerHTML = Math.floor(numOfYears);
        document.querySelector("#num-of-months").innerHTML = Math.floor(numOfMonths);
        document.querySelector("#num-of-days").innerHTML = Math.floor(numOfDays);
    }

This approach has a margin of error of a few days, so I'm wondering if there is a better approach to make the calculation more accurate. Would it be better to actually find the distance in days between two calendar dates than to calculate it using milliseconds?

Code
Loading...

Please log in to post a comment

Log in with GitHub

Community feedback

No feedback yet. Be the first to give feedback on Daniel Aadland's solution.

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner
  • Use cases

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License