Age Calculator with HTML, CSS & JavaScript
30 DAYS 30 PROJECT CHALLENGE
Day #18
Project Overview
An Age Calculator built using HTML, CSS, and JavaScript that calculates a user’s exact age based on their date of birth.
The application computes age in years, months, and days, validates user input, and displays results instantly with a clean and user-friendly interface.
Key Features
- Calculates age in years, months, and days accurately.
- Date of birth selection using a date picker.
- Validates input to prevent empty or future dates.
- Displays results instantly without page reload.
- Clean, responsive, and easy-to-use design.
- Suitable for learning date manipulation in JavaScript.
HTML Code
<!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="style.css" />
</head>
<body>
<div class="calculator">
<h2>Age Calculator</h2>
<label for="dob">Select your Date of Birth</label>
<input type="date" id="dob" />
<button onclick="calculateAge()">Calculate Age</button>
<div class="result" id="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code
* {
box-sizing: border-box;
font-family: "Segoe UI", sans-serif;
}
body {
min-height: 100vh;
background: #042453; /* DO NOT CHANGE */
display: flex;
justify-content: center;
align-items: center;
}
.calculator {
background: #ffffff;
width: 320px;
padding: 25px;
border-radius: 12px;
text-align: center;
box-shadow: 0 15px 30px rgba(0,0,0,0.2);
}
h2 {
margin-bottom: 15px;
}
label {
font-size: 14px;
}
input {
width: 100%;
padding: 10px;
margin: 10px 0;
border-radius: 6px;
border: 1px solid #ccc;
}
button {
width: 100%;
padding: 10px;
border: none;
border-radius: 6px;
background: #4f46e5;
color: white;
cursor: pointer;
}
.result {
margin-top: 15px;
font-size: 14px;
font-weight: 600;
}
Javascript Code
function calculateAge() {
const dobInput = document.getElementById("dob").value;
const result = document.getElementById("result");
if (!dobInput) {
result.style.color = "red";
result.innerText = "Please select your date of birth";
return;
}
const dob = new Date(dobInput);
const today = new Date();
if (dob > today) {
result.style.color = "red";
result.innerText = "Date of birth cannot be in the future";
return;
}
let years = today.getFullYear() - dob.getFullYear();
let months = today.getMonth() - dob.getMonth();
let days = today.getDate() - dob.getDate();
if (days < 0) {
months--;
days += new Date(today.getFullYear(), today.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
result.style.color = "green";
result.innerText = `You are ${years} years, ${months} months, and ${days} days old.`;
}
Related Projects
Day 16 : Shopping Cart (Add / Remove / Total)
Interactive cart with real-time item management and total calculation.
Concepts: Cart state handling, dynamic price calculation, user interaction logic.
Day 20 : Form Auto-Save (Local Storage)
Automatically saves form input data and restores it on reload.
Concepts: LocalStorage usage, input event handling, persistent UI state.
Day 21 : Country Info Finder (API)
Fetches and displays real-time country information using a public REST API.
Concepts: API integration, fetch API, async data handling, dynamic UI rendering.