screen_rotation
Copied to Clipboard
<!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> </head> <body> <h2>Javascript Calculate date of birth (age) and Validation</h2> <label for="dob">Enter Date of Birth:</label> <input type="date" id="dob" required> <button onclick="calculateAge()">Calculate Age</button> <p id="result"></p> <script> function calculateAge() { const dobInput = document.getElementById('dob'); const resultElement = document.getElementById('result'); const today = new Date(); const birthDate = new Date(dobInput.value); if (birthDate > today) { resultElement.textContent = "Invalid date of birth. Please enter a valid date."; return; } let ageInMilliseconds = today - birthDate; // Calculate years const years = Math.floor(ageInMilliseconds / (365.25 * 24 * 60 * 60 * 1000)); // Calculate months const months = Math.floor((ageInMilliseconds % (365.25 * 24 * 60 * 60 * 1000)) / (30.44 * 24 * 60 * 60 * 1000)); // Calculate days const days = Math.floor((ageInMilliseconds % (30.44 * 24 * 60 * 60 * 1000)) / (24 * 60 * 60 * 1000)); resultElement.textContent = `Age: ${years} years, ${months} months, ${days} days`; } </script> <style> body { font-family: 'Arial', sans-serif; background-color: #f4f4f4; text-align: center; margin: 50px; } h2 { color: #333; } label { font-size: 18px; margin-top: 20px; display: block; } input { padding: 12px; font-size: 16px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 4px; width: 200px } button { background-color: #4caf50; color: white; padding: 12px 24px; font-size: 16px; cursor: pointer; border: none; border-radius: 4px; } button:hover { background-color: #45a049; } p { font-size: 20px; color: #333; margin-top: 20px; padding: 10px; } </style> </body> </html>