'get a date from age in javascript [duplicate]
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
Solution 1:[1]
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/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
