diff --git a/AgeCalculator/raamrutsagarb25-tech/README.md b/AgeCalculator/raamrutsagarb25-tech/README.md new file mode 100644 index 000000000..66af915c1 --- /dev/null +++ b/AgeCalculator/raamrutsagarb25-tech/README.md @@ -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/` +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) diff --git a/AgeCalculator/raamrutsagarb25-tech/index.html b/AgeCalculator/raamrutsagarb25-tech/index.html new file mode 100644 index 000000000..fa347da67 --- /dev/null +++ b/AgeCalculator/raamrutsagarb25-tech/index.html @@ -0,0 +1,36 @@ + + + + + + Age Calculator + + + +
+

Age Calculator

+

Enter your date of birth to see your exact age.

+ + + + + + + + + +
+ + + + diff --git a/AgeCalculator/raamrutsagarb25-tech/screenshot.png b/AgeCalculator/raamrutsagarb25-tech/screenshot.png new file mode 100644 index 000000000..afb5d4a4f Binary files /dev/null and b/AgeCalculator/raamrutsagarb25-tech/screenshot.png differ diff --git a/AgeCalculator/raamrutsagarb25-tech/script.js b/AgeCalculator/raamrutsagarb25-tech/script.js new file mode 100644 index 000000000..1557d5d20 --- /dev/null +++ b/AgeCalculator/raamrutsagarb25-tech/script.js @@ -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(); +}); diff --git a/AgeCalculator/raamrutsagarb25-tech/style.css b/AgeCalculator/raamrutsagarb25-tech/style.css new file mode 100644 index 000000000..56d09c3e3 --- /dev/null +++ b/AgeCalculator/raamrutsagarb25-tech/style.css @@ -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; }