Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions AgeCalculator/raamrutsagarb25-tech/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Age Calculator

A simple age calculator built with HTML, CSS and vanilla JavaScript. Enter your date of birth and it shows your exact age in years, months and days, the total days you have lived, and how many days remain until your next birthday.

## Tech used
- HTML5
- CSS3
- JavaScript (no frameworks, no libraries, no API keys)

## How to run locally
1. Clone the repository and open this folder: `AgeCalculator/<your-username>`
2. Open `index.html` in any browser. No install or build step needed.

## Features
- Exact age in years, months and days
- Total days lived
- Days until next birthday
- Validation for empty and future dates
- Responsive layout

## Screenshot
![Age Calculator screenshot](screenshot.png)
36 changes: 36 additions & 0 deletions AgeCalculator/raamrutsagarb25-tech/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!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>
<main class="card">
<h1>Age Calculator</h1>
<p class="sub">Enter your date of birth to see your exact age.</p>

<label for="dob">Date of birth</label>
<input type="date" id="dob" />

<button id="calc">Calculate</button>

<p id="error" class="error" role="alert"></p>

<section id="result" class="result hidden">
<div class="age">
<div><span id="years">0</span><small>Years</small></div>
<div><span id="months">0</span><small>Months</small></div>
<div><span id="days">0</span><small>Days</small></div>
</div>
<ul>
<li>Total days lived: <strong id="totalDays">0</strong></li>
<li>Next birthday in: <strong id="nextBday">0</strong> day(s)</li>
</ul>
</section>
</main>

<script src="script.js"></script>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions AgeCalculator/raamrutsagarb25-tech/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const dobInput = document.getElementById("dob");
const calcBtn = document.getElementById("calc");
const errorEl = document.getElementById("error");
const resultEl = document.getElementById("result");

const MS_PER_DAY = 24 * 60 * 60 * 1000;

// Parse "YYYY-MM-DD" as a local date (avoids timezone shifts)
function parseDate(value) {
const [y, m, d] = value.split("-").map(Number);
return new Date(y, m - 1, d);
}

// Exact age in years, months and days
function calculateAge(dob, today) {
let years = today.getFullYear() - dob.getFullYear();
let months = today.getMonth() - dob.getMonth();
let days = today.getDate() - dob.getDate();

if (days < 0) {
months--;
// days in the previous month
days += new Date(today.getFullYear(), today.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
return { years, months, days };
}

function daysUntilNextBirthday(dob, today) {
const next = new Date(today.getFullYear(), dob.getMonth(), dob.getDate());
if (next < today) next.setFullYear(today.getFullYear() + 1);
return Math.round((next - today) / MS_PER_DAY);
}

function showError(message) {
errorEl.textContent = message;
resultEl.classList.add("hidden");
}

function handleCalculate() {
errorEl.textContent = "";

if (!dobInput.value) {
showError("Please select your date of birth.");
return;
}

const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const dob = parseDate(dobInput.value);

if (dob > today) {
showError("Date of birth cannot be in the future.");
return;
}

const { years, months, days } = calculateAge(dob, today);

document.getElementById("years").textContent = years;
document.getElementById("months").textContent = months;
document.getElementById("days").textContent = days;
document.getElementById("totalDays").textContent = Math.floor(
(today - dob) / MS_PER_DAY
).toLocaleString();
document.getElementById("nextBday").textContent = daysUntilNextBirthday(dob, today);

resultEl.classList.remove("hidden");
}

// Block future dates in the picker
dobInput.max = new Date().toISOString().split("T")[0];

calcBtn.addEventListener("click", handleCalculate);
dobInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") handleCalculate();
});
66 changes: 66 additions & 0 deletions AgeCalculator/raamrutsagarb25-tech/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-family: "Segoe UI", Arial, sans-serif;
background: linear-gradient(135deg, #4f46e5, #06b6d4);
padding: 16px;
}

.card {
width: 100%;
max-width: 420px;
background: #fff;
border-radius: 16px;
padding: 28px;
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.2);
}

h1 { font-size: 1.6rem; color: #1f2937; margin-bottom: 4px; }
.sub { color: #6b7280; margin-bottom: 20px; font-size: 0.95rem; }

label { display: block; font-weight: 600; margin-bottom: 6px; color: #374151; }

input[type="date"] {
width: 100%;
padding: 10px 12px;
border: 1px solid #d1d5db;
border-radius: 8px;
font-size: 1rem;
margin-bottom: 14px;
}

button {
width: 100%;
padding: 11px;
border: none;
border-radius: 8px;
background: #4f46e5;
color: #fff;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
button:hover { background: #4338ca; }

.error { color: #dc2626; min-height: 1.2em; margin-top: 10px; font-size: 0.9rem; }

.result { margin-top: 16px; }
.hidden { display: none; }

.age { display: flex; gap: 10px; margin-bottom: 14px; }
.age div {
flex: 1;
text-align: center;
background: #eef2ff;
border-radius: 10px;
padding: 12px 4px;
}
.age span { display: block; font-size: 1.8rem; font-weight: 700; color: #4f46e5; }
.age small { color: #6b7280; }

ul { list-style: none; color: #374151; line-height: 1.8; }