Design comparison
SolutionDesign
Solution retrospective
i couldn't figure out how to display the current month and days of age so i showed instead the total months and days someone lived cant wait to see other people code and figure it out thank you i could finish this up cleanly but im challenging myself to keep everything vanilla js css and html
Community feedback
- @manjubhaskar02Posted 10 months ago
Try this one .
function calculateAge(event) { event.preventDefault(); const birthdateInput = document.getElementById('birthdate'); const resultElement = document.getElementById('result'); const birthDate = new Date(birthdateInput.value); const currentDate = new Date(); let years = currentDate.getFullYear() - birthDate.getFullYear(); let months = currentDate.getMonth() - birthDate.getMonth(); let days = currentDate.getDate() - birthDate.getDate(); // Adjust negative months or days if (days < 0) { months--; days += new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate(); } if (months < 0) { years--; months += 12; } //and display the result.
Marked as helpful1@winston9797Posted 10 months ago@manjubhaskar02 thank you so much this solution works way better and its simpler than other ways to calculate I found online
0 - @winston9797Posted 10 months ago
Update : thanks to @manjubhaskar02 I was able to fix the issue with the app and display accurate data this community is awesome
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord