Age Calculator HTML
Prompt
Create HTML code for an age calculator. The calculator should allow a user to input a date of birth and then display the calculated age in years, months, and days. # Steps 1. **HTML Structure**: Design the form to capture the user's input, specifically the date of birth. - Use appropriate input types for capturing the date. - Include a submit button to trigger the calculation. 2. **JavaScript Logic**: Implement the date computation. - On submission, read the input value. - Calculate the age by comparing the current date to the date of birth. - Break down the resulting age into years, months, and days. 3. **Display the Result**: Show the computed age back to the user. - Ensure the result is presented in a clear and readable format below the input form. # Output Format - Provide an HTML file structure with inline JavaScript that functions correctly as a standalone webpage. - Ensure functionality is self-contained with no external dependencies. # Example ```html <!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> <style> body { font-family: Arial, sans-serif; } .container { max-width: 300px; margin: auto; text-align: center; padding: 50px; } </style> </head> <body> <div class="container"> <h1>Calculate Your Age</h1> <form id="age-form"> <label for="dob">Enter your date of birth:</label><br><br> <input type="date" id="dob" name="dob"><br><br> <input type="button" value="Calculate Age" onclick="calculateAge()"> </form> <h2 id="result"></h2> </div> <script> function calculateAge() { var dob = document.getElementById('dob').value; if(!dob) { document.getElementById('result').innerText = "Please enter your date of birth."; return; } var dobDate = new Date(dob); var today = new Date(); var age = today.getFullYear() - dobDate.getFullYear(); var monthDiff = today.getMonth() - dobDate.getMonth(); if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < dobDate.getDate())) { age--; } var yearDiff = age; // Calculate months var months = monthDiff < 0 ? 12 + monthDiff : monthDiff; if (today.getDate() < dobDate.getDate()) { months--; } // Calculate days var days = today.getDate() >= dobDate.getDate() ? today.getDate() - dobDate.getDate() : new Date(today.getFullYear(), today.getMonth(), 0).getDate() - dobDate.getDate() + today.getDate(); document.getElementById('result').innerText = `You are ${yearDiff} years, ${months} months, and ${days} days old.`; } </script> </body> </html> ```
Related Coding Prompts
Write Code
As a seasoned programmer, your task is to write code in [programming language] to [perform action]. The code should be efficient, well-structured, and optimized for performance. Make sure to follow best practices and industry standards while implementing the necessary algorithms and logic to achieve the desired functionality. Test the code thoroughly to ensure it functions as intended and meets all requirements. Additionally, document the code properly for future reference and maintenance.
Debug Code
Act as a seasoned programmer with over 20 years of commercial experience. Analyze the provided [piece of code] that is causing a specific [error]. Your task involves diagnosing the root cause of the error, understanding the context and functionality intended by the code, and proposing a solution to fix the issue. Your analysis should include a step-by-step walkthrough of the code, identification of any bugs or logical mistakes, and a detailed explanation of how to resolve them. Additionally, suggest any improvements or optimizations to enhance the performance, readability, or maintainability of the code based on your extensive experience. Ensure that your solution adheres to best practices in software development and is compatible with the current development environment where the code is being executed.
Do Code Review
As a seasoned programmer with over 20 years of commercial experience, your task is to perform a comprehensive code review on the provided [piece of code]. Your review should meticulously evaluate the code's efficiency, readability, and maintainability. You are expected to identify any potential bugs, security vulnerabilities, or performance issues and suggest specific improvements or optimizations. Additionally, assess the code's adherence to industry standards and best practices. Your feedback should be constructive and detailed, offering clear explanations and recommendations for changes. Where applicable, provide examples or references to support your suggestions. Your goal is to ensure that the code not only functions as intended but also meets high standards of quality and can be easily managed and scaled in the future. This review is an opportunity to mentor and guide less experienced developers, so your insights should be both educational and actionable.