Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
get a date from age in javascript

I'm trying to show the date of birth when the user puts their age, date and month will be current, for this example 12 here present age

     var d = new Date();
     var newdate = (d.getFullYear()-12)+'-'+(d.getMonth()+1)+'-'+d.getDate();
     document.getElementById("age").value =newdate;

if I put the static values it shows on the HTML field (age)

document.getElementById("age").value ="2022-04-09";

but also if I try to change the input type from date to text then it shows the value

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Changing this as I miss understood the question, what you want to do is get the date from age.

Basic HTML Setup

<input id="age" type="text">
<input type="button" onClick="javascript:yearCalculate()" value="Calculate Birth Date">
<p>
You were born on: <span id="birthDate"></span>
</p>

And your javascript code

function yearCalculate() {
    // set Birthday and get todays date
  const birthDay = new Date();
  
  // get age 
  const ageYear = document.getElementById('age').value;

    // update year
  birthDay.setFullYear(birthDay.getFullYear() - ageYear);
  
  document.getElementById('birthDate').innerHTML = birthDay;
}

JSFiddle: https://jsfiddle.net/Lfrad2t6/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda