Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda