0

my question about html & JS and date I want make formula calculate the age of person

the user will input his/her age I think with <input type="date"/> tag and JV will calculate the age according this formula year = 2020 - (input year) month = 12 - (input month) day = 30 - (input day) what I know is the all websites use some way to calculate age with the user input birthday. I wrote all program but just I'm stuck in this code thank you

16th Mar 2020, 6:46 PM
Soumaiya Anwer
Soumaiya Anwer - avatar
2 Réponses
0
try this: (This is not accurate because not all months have 30 days) var date = document.getElementById("date"); var sub = document.getElementById("submit"); sub.addEventListener("click", calc); function calc() { var currentDate = new Date(); var dateArray = date.value.split("-") console.log(currentDate) var years = currentDate.getFullYear() - dateArray[0]; var months = currentDate.getMonth() + 1 - dateArray[1]; var days = currentDate.getDate() - dateArray[2]; if(days < 0) { days = 30 + days; months--; }; if(months < 0) { months = 12 + months; years--; }; alert("you are " + years + " years " + months + " months and " + days + " days old." ) }
16th Mar 2020, 7:36 PM
yochanan sheinberger
yochanan sheinberger - avatar
+ 1
thank you so much it's work yochanan sheinberger 🤗
17th Mar 2020, 9:28 PM
Soumaiya Anwer
Soumaiya Anwer - avatar