I am trying to create a Birthday Calculator that also includes months and days, not just years.
However I can't seem to get it to post the answer, and when it was posting it was incorrect.
I'm still learning, so I would really appreciate any feedback, and I'm hoping it is something simple that I overlooked.
//birthday (entered by user)
function calculateDiff(){
var FromYear = parseInt(date[2]);
var FromMonth = parseInt(date[1]);
var FromDay = parseInt(date[0]);
//current date
var ToYear = today.getFullYear();
var ToMonth = today.getMonth() + 1;
var ToDay = new Date();
var years, months, days;
//months
months = ToMonth - FromMonth;
if(ToDay < FromDay) {
months = months -1;
}
//years
years = ToYear = FromYear;
if(ToMonth < FromMonth) {
years = years - 1;
months = months + 12;
}
//days
days = Math.floor((ToDay.getTime() - (newDate(FromYear + years, FromMonth + months - 1, FromDay)).getTime()) / (24 * 60 * 60 * 1000));
//answer order
var answer = years + " years, " + months + " months, and " + days + " days.";
//post answer
document.getElementById("result").textContent = answer;
return {years: years, months: months, days: days};
}
<table>
<tbody>
<tr class="header">
<th> </th>
<th>Year</th>
<th>Month</th>
<th>Day</th>
</tr>
<tr>
<th>Birth Date</th>
<td><input id="FromYear" placeholder="YYYY" value=""></td>
<td><input id="FromMonth" placeholder="MM" value=""></td>
<td><input id="FromDay" placeholder="DD" value=""></td>
</tr>
<tr>
<th colspan="4">
<button id="calculateDiff">Calculate</button>
</th>
</tr>
<tr colspan="4">
<th>You are <div id="result"></div></th>
</tr>
</tbody>
</table>