DEV Community

Cover image for Age Calculator Using HTML, CSS, and JavaScript: A Beginner’s Guide
Javacodepoint
Javacodepoint

Posted on

Age Calculator Using HTML, CSS, and JavaScript: A Beginner’s Guide

Let’s start by creating the structure of our age calculator using HTML. Open your text editor and create a new HTML file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Age Calculator</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <h1>Age Calculator</h1>
        <div class="form">
            <label for="dob">Date of Birth:</label>
            <input type="date" id="dob">
            <button onclick="calculateAge()">Calculate Age</button>
        </div>
        <div class="result">
            <p>Your age is:</p>
            <p id="ageResult">0 years, 0 months, 0 days</p>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Styling with CSS

To make our age calculator visually appealing, let’s add some CSS to a separate styles.css file. Here’s a simple CSS stylesheet to get you started:

body {
    font-family: Arial, sans-serif;
    background-color: #f5f5f5;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.container {
    background-color: #fff;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    text-align: center;
}

h1 {
    color: #333;
}

.form {
    margin: 20px 0;
}

label {
    font-weight: bold;
}

input[type="date"] {
    padding: 5px;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 16px;
}

button {
    background-color: #007BFF;
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;
}

.result {
    margin-top: 20px;
    font-size: 18px;
}
Enter fullscreen mode Exit fullscreen mode

Calculating Age with JavaScript

Now, let’s add the functionality to calculate the age using JavaScript. Create a script.js file and add the following code:

function calculateAge() {
    const dobInput = document.getElementById('dob').value;
    const dob = new Date(dobInput);
    const currentDate = new Date();

    const ageInMilliseconds = currentDate - dob;
    const ageInYears = ageInMilliseconds / (365 * 24 * 60 * 60 * 1000);
    const age = Math.floor(ageInYears);

    currentDate.setFullYear(currentDate.getFullYear() - age);
    const monthDiff = currentDate.getMonth() - dob.getMonth();

    let months = monthDiff;
    if (currentDate.getDate() < dob.getDate()) {
        months--;
    }

    let days = currentDate.getDate() - dob.getDate();
    if (days < 0) {
        const lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();
        days = lastDayOfMonth - dob.getDate() + currentDate.getDate();
    }

    document.getElementById('ageResult').textContent = `${age} years, ${months} months, ${days} days`;
}
Enter fullscreen mode Exit fullscreen mode

Learn more in detail with complete source code with code explanation here: Age Calculator Using HTML, CSS, and JavaScript: A Beginner’s Guide

Top comments (3)

Collapse
 
alexedbalet profile image
alexedbalet

Creating an age calculator using HTML, CSS, and JavaScript is a great project for beginners to understand web development fundamentals. This simple tool allows users to input their birthdate and calculates their exact age by leveraging JavaScript functions for date manipulation. By integrating HTML for structure and CSS for styling, you can build a user-friendly interface that visually presents the age calculate results. Just as an "age calculator" accurately determines age based on specific input data, this guide helps you develop a functional and visually appealing age calculation tool, enhancing your coding skills and understanding of web technologies.

Collapse
 
yashesaplama profile image
Yaş Hesaplama

Great tutorial for beginners looking to build an Age Calculator using HTML, CSS, and JavaScript! The step-by-step approach makes it easy to follow, and the styling adds a nice touch. For a more advanced and user-friendly age calculation experience, check out Age Calculator.

Collapse
 
tnia_posh_d9933578f3b5a28 profile image
tnia posh

Best Age Calculator Online – Accurate, Free & Easy to Use

If you're looking for a reliable age calculator online, you're in the right place. An age calculator is a simple yet powerful tool that lets users instantly determine their exact age in years, months, and days based on their date of birth. Whether you're tracking birthdays, planning events, or just curious, an age calculator free online offers the convenience and speed you need.

Key Features of a Good Online Age Calculator:
Instant Results: Just enter your birthdate and get your exact age in seconds.

Free to Use: Most reputable tools are completely free with no hidden costs.

User-Friendly Interface: No technical skills required — just input and go.

No Signup Needed: Use the tool without creating an account or sharing personal details.

Accurate Calculations: Get precise age breakdowns, including months, days, and even minutes.

Benefits of Using an Online Age Calculator:
Perfect for filling out online forms, creating resumes, or checking eligibility for age-specific services.

Useful for school projects, government forms, visa applications, and more.

Great for fun too — find out how old you are in days or minutes!

Recommended Uses:
Age calculator by date of birth

Current age calculator

Birthday calculator online

Date of birth to age converter

Exact age calculator free online

Final Thoughts:
Whether you're calculating your age for official documentation or just out of curiosity, an age calculator online free tool is a must-have utility. It's fast, accurate, and hassle-free. Bookmark a good one and you’ll always have the answer to “How old am I?” right at your fingertips.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.