Basically I have to find out how old someone is in months, years and days. I have worked out the days and years but not the months as there're leap years and some months are different in length.
Here is my code
//Variables
var now = new Date();
var totalDays = '';
var totalMonths = '';
var totalWeeks;
var birthDate = new Date(document.getElementById('selYear').value, document.getElementById('selMonth').value, document.getElementById('selDay').value);
var output = '';
//Total Days Old
var totalDays = Math.floor((now - birthDate) / 86400000);
//Total Weeks Old
totalWeeks = totalDays / 7;
totalWeeks = Math.round(totalWeeks);
//Total Months Old
totalMonths = totalDays / 30;
//Total Years
var totalYears = document.getElementById('selYear').value - now.getFullYear();
totalYears = Math.abs(totalYears);
//Outputs
output += 'You are ' + totalDays + ' days old';
output += '<br />You are ' + totalWeeks + ' weeks old';
output += '<br />You are ' + totalMonths + ' months old';
output += '<br />You are ' + totalYears + ' Years old';
I've thought of using if statements or a for loop and i've searched all of google and I can't find a way!
I have an application that asks the user for day, month and year. I have tested my current code (totalMonths = totalDays / 30) and it will get close to the totalMonths that it's suppose to be (I have checked an online site) and the date [1/1/1990] will fetch 302 while it should fetch 297 months