<div class="container"> <h2>Date of Birth</h2> <form id="dobForm"> <div class="form-group"> <label for="dob">Select your date of birth</label> <input type="date" id="dob" name="dob" required> <div class="error" id="error"></div> </div> <button type="submit">Calculate Age</button> </form> <div class="result" id="result"> <div class="result-text" id="resultText"></div> </div> </div>
* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; background: linear-gradient(135deg, #00c6ff 0%, #0072ff 100%); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; } .container { background: white; border-radius: 12px; padding: 40px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); max-width: 400px; width: 100%; } h2 { color: #333; margin-bottom: 24px; font-size: 24px; font-weight: 600; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; color: #555; font-size: 14px; font-weight: 500; } input[type="date"] { width: 100%; padding: 12px 16px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 16px; color: #333; transition: all 0.3s ease; background: white; } input[type="date"]:focus { outline: none; border-color: #00c6ff; box-shadow: 0 0 0 3px rgba(0, 198, 255, 0.1); } input[type="date"]:hover { border-color: #00c6ff; } button { width: 100%; padding: 14px; background: linear-gradient(135deg, #00c6ff 0%, #0072ff 100%); color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: 600; cursor: pointer; transition: transform 0.2s ease, box-shadow 0.2s ease; } button:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(0, 198, 255, 0.4); } button:active { transform: translateY(0); } .result { margin-top: 20px; padding: 16px; background: #f5f7fa; border-radius: 8px; border-left: 4px solid #00c6ff; display: none; } .result.show { display: block; animation: slideIn 0.3s ease; } @keyframes slideIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } } .result-text { color: #333; font-size: 14px; line-height: 1.6; } .result-text strong { color: #0072ff; } .error { color: #e74c3c; font-size: 13px; margin-top: 8px; display: none; } .error.show { display: block; }
const form = document.getElementById('dobForm'); const dobInput = document.getElementById('dob'); const result = document.getElementById('result'); const resultText = document.getElementById('resultText'); const error = document.getElementById('error'); const today = new Date().toISOString().split('T')[0]; dobInput.setAttribute('max', today); const minDate = new Date(); minDate.setFullYear(minDate.getFullYear() - 120); dobInput.setAttribute('min', minDate.toISOString().split('T')[0]); form.addEventListener('submit', (e) => { e.preventDefault(); error.classList.remove('show'); const dob = new Date(dobInput.value); const now = new Date(); if (!dobInput.value) { showError('Please select a date of birth'); return; } if (dob > now) { showError('Date of birth cannot be in the future'); return; } const age = calculateAge(dob, now); resultText.innerHTML = ` <strong>Date of Birth:</strong> ${formatDate(dob)}<br> <strong>Age:</strong> ${age.years} years, ${age.months} months, and ${age.days} days `; result.classList.add('show'); }); function calculateAge(dob, now) { let years = now.getFullYear() - dob.getFullYear(); let months = now.getMonth() - dob.getMonth(); let days = now.getDate() - dob.getDate(); if (days < 0) { months--; const lastMonth = new Date(now.getFullYear(), now.getMonth(), 0); days += lastMonth.getDate(); } if (months < 0) { years--; months += 12; } return { years, months, days }; } function formatDate(date) { const options = { year: 'numeric', month: 'long', day: 'numeric' }; return date.toLocaleDateString('en-US', options); } function showError(message) { error.textContent = message; error.classList.add('show'); result.classList.remove('show'); }
Login to leave a comment
No comments yet. Be the first!
No comments yet. Be the first!