Age Calculator in JavaScript: Calculate Age from Date of Birth

How to create to create an age calculator in JavaScript that calculates Age from Date of Birth?

Read on, because we will hand you a step-by-step tutorial on developing a simple age calculator in JavaScript.

Let’s dive in!

How to calculate age from date of birth in JavaScript?

// Function to calculate age
function calculateAge(birthDate, otherDate) {
    birthDate = new Date(birthDate);
    otherDate = new Date(otherDate);

    let years = (otherDate.getFullYear() - birthDate.getFullYear());

    if (otherDate.getMonth() < birthDate.getMonth() ||
        otherDate.getMonth() == birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) {
        years--;
    }

    return years;
}

// Usage
let birthDate = "08/13/1993"; // Format: MM/DD/YYYY
let otherDate = new Date(); // Current date

let age = calculateAge(birthDate, otherDate);

console.log("The calculated age is: " + age);

The code above will calculate the age based on the provided birth date and the current date.

Then, it will logs the calculated age to the console. You can replace new Date() with any other date you want to calculate the age at that specific date.

Remember to use the same date format as the birth date.

Output:

30

How to create a simple age calculator in JavaScript?

Here’s a step-by-step guide to creating a simple age calculator using JavaScript and HTML:

Step 1: Create the HTML structure

We need to create the HTML structure first for our age calculator. This will include an input field for the date of birth and a button to calculate the age.

<!DOCTYPE html>
<html>
<head>
    <title>Age Calculator</title>
</head>
<body>
  <h1>How to create age calculator in JavaScript</h1>
    <label for="dob">Enter your Date of Birth:</label><br>
    <input type="date" id="dob" name="dob">
    <button onclick="calculateAge()">Calculate Age</button><br>
    <p id="result"></p>
</body>
</html>

Step 2: Write the JavaScript function

After we had created the HTML structure, we needed to write a JavaScript function that calculates the age based on the date of birth.

function calculateAge() {
    var dob = new Date(document.getElementById('dob').value);
    var diff_ms = Date.now() - dob.getTime();
    var age_dt = new Date(diff_ms); 

    var age = Math.abs(age_dt.getUTCFullYear() - 1970);

    document.getElementById('result').textContent = 'Age is ' + age;
}

This function gets the value from the date input field, calculates the age, and then displays the result in the paragraph element with id result.

Here’s the complete code:

<!DOCTYPE html>
<html>
<head>
    <title>Age Calculator</title>
</head>
<body>
    <h1>How to create age calculator in JavaScript</h1>
    <label for="dob">Enter your Date of Birth:</label><br> <br>
    <input type="date" id="dob" name="dob">
    

    <button onclick="calculateAge()">Calculate Age</button><br>
    <p id="result"></p>

    <script type="text/javascript">
        function calculateAge() {
            var dob = new Date(document.getElementById('dob').value);
            var diff_ms = Date.now() - dob.getTime();
            var age_dt = new Date(diff_ms); 

            var age = Math.abs(age_dt.getUTCFullYear() - 1970);

            document.getElementById('result').textContent = 'Age is ' + age;
        }
    </script>

</body>
</html>

Output:

Conclusion

In conclusion, we’ve provided two examples for creating an age calculator in JavaScript.

The first one involves a JavaScript function calculateAge() that calculates the age based on a provided birth date and the current date.

The second example demonstrates how to create a simple age calculator with an HTML structure, an input field for the date of birth, and a button to trigger the age calculation using JavaScript.

Both examples are effective ways to calculate and display a person’s age based on their date of birth.

We hope this article has provided you with enough information on how to create the age calculator in JavaScript.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment