Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 1 year ago

Age Calculator - JS CSS HTML

LucaSofronie•70
@LucaSofronie
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 a lot with getting the correct time passed since a date because I didn't know what the algorithm should be like, so in the end I chose to count the days between the two dates and divide them by 365.25 to get the years etc. What do you think was the correct approach? Also, I struggled with the 'years', 'months' and 'days' elements in HTML and CSS due to their big font size (they were overflowing). How should I do it in a better way?

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • P
    Johnny Gérard•880
    @johnnygerard
    Posted over 1 year ago

    The simplest way to get the age is to compute a date difference between today and the birthdate.

    Let me give you an example:

    Pretend today is 2024-01-23 and the user's birthdate is 2000-02-28.

    Compute the year difference: 2024 - 2000 = 24. It will never be negative as long as the birthdate is in the past.

    Compute the month difference: 1 - 2 = -1. Because we have a negative value, we need to borrow a year and add 12 months.

    Currently the computed age is: 23 years, 11 months and ? days.

    Let us now compute the day difference: 23 - 28 = -5. Again a negative value, let us borrow a month and add the number of days in the previous month (December 2023).

    When borrowing a month, we need to check for a negative value and borrow a year and add 12 months again in that case.

    To get the number of days in the previous month, we can use this JavaScript expression: new Date(2024, 0, 0).getDate(). Note that the second argument (month) is zero-based and the third argument (day) is one-based. By using 0 instead of 1 for the third argument, we get the last day of the previous month instead of the first day of the current month.

    We can now add 31 to -5 which is 26. The computed age is 23 years, 10 months and 26 days.

    We can confirm this by adding the values to the birthdate.

    Birthdate: 2000-02-28

    +23 years: 2023-02-28

    +10 months: 2023-12-28

    +26 days: 2023-12-54

    We can evaluate new Date(2023, 11, 54).toDateString() which gives 'Tue Jan 23 2024'.

    Here my TypeScript code for this:

    static computeAge(year: number, month: number, day: number): Age {
        const today = new Date;
        const currentYear = today.getFullYear();
        const currentMonth = today.getMonth();
        const currentDay = today.getDate();
    
        let years = currentYear - year;
        let months = currentMonth - month;
        let days = currentDay - day;
    
        if (months < 0) {
          // Borrow a year
          years--;
          months += 12;
        }
    
        if (days < 0) {
          // Borrow a month
          months--;
          days += this.getPreviousMonthDays(currentYear, currentMonth);
    
          if (months < 0) {
            // Borrow a year
            years--;
            months += 12;
          }
        }
    
        return { years, months, days };
      }
    
      /**
     * @param month Zero-based month
     * @param year Year
     * @returns Total number of days in the previous month
     */
      private static getPreviousMonthDays(year: number, month: number): number {
        // Day 0 is shifted to the previous month's last day
        return new Date(year, month, 0).getDate();
      }
    

    Age calculator app solution

    Marked as helpful

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
Frontend Mentor logo

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

Frontend Mentor

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

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

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub