-2

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

  • `let d = new Date(); d.setFullYear(d.getFullYear() - age); console.log(d.toLocaleDateString('en-CA'));` – RobG Apr 09 '22 at 21:40

1 Answers1

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/