0
I have this function, but when rotates the month is always giving +12 months, as if the person had already made birthday. How could fix?
let daysH = document.getElementById("date");
let monthH = document.getElementById("month");
let yearH = document.getElementById("year");
let showAge = document.getElementById("show-age");
const btn = document.getElementById("calc-btn");
var today = new Date();
let d = today.getDate();
var m = today.getMonth() + 1;
var y = today.getFullYear();
let maxDays = 0;
let monthNum = 1;
btn.addEventListener("click", (days, month, year) => {
  days = daysH.value;
  month = monthH.value;
  year = yearH.value;
  if (daysH.value == "" || monthH.value == "" || yearH.value == "") {
    return alert("Por favor, preencha seu dados")
  }
  // no else condition needed
  console.log(`Current Date : ${d} ${m} ${y}`);
  if (m == 1) { // janeiro
    maxDays = 31;
    monthNum = 1;
  } else if (m == 2) { // fevereiro
    maxDays = 28;
    monthNum = 2;
  } else if (m == 3) { // março
    maxDays = 31;
    monthNum = 3;
  } else if (m == 4) { // abril
    maxDays = 30;
    monthNum = 4;
  } else if (m == 5) { // maio
    maxDays = 31;
    monthNum = 5;
  } else if (m == 6) { // junho
    maxDays = 30;
    monthNum = 6;
  } else if (m == 7) { // julho
    maxDays = 31;
    monthNum = 7;
  } else if (m == 8) { // agosto
    maxDays = 31;
    monthNum = 8;
  } else if (m == 9) { // setembro
    maxDays = 30;
    monthNum = 9;
  } else if (m == 10) { // outubro
    maxDays = 31;
    monthNum = 10;
  } else if (m == 11) { // novembro
    maxDays = 30;
    monthNum = 11;
  } else { //dezembro
    maxDays = 31;
    monthNum = 12;
  }
  var ageYears = y - year;
  if (m <= month) {
    m = m + 12;
  }
  var ageMonths = m - month;
  var ageDays = (maxDays - d) + parseInt(days);
  console.log(`Sua Idade é ${ageYears} a , ${ageMonths} m and ${ageDays} d`);
  showAge.innerHTML = `Sua Idade é ${ageYears} a , ${ageMonths} m and ${ageDays} d`;
})<body>
  <label for="input">Qual a data do seu nascimento? </label>
  <input type="number" id="date" placeholder="data">
  <input type="number" id="month" placeholder="mês">
  <input type="number" id="year" placeholder="ano">
  <button id="calc-btn">Calculate Age</button>
  <p id="show-age"></p>
</body>
so I need you to calculate the months and days too
– maria pazuelo
The Stack Overflow in English is not a forum, we are a Q&A site. Do not use the answer field to exchange communication. Or edit the question adding details and clarification or use the chat for more active communication.
– Augusto Vasques