As you are working with seconds you would do best to use the getTime() method which returns the number of
milliseconds between midnight of January 1, 1970 and the specified date.
You can then compare the value for now with the user's birthdate and convert the difference to days, hours, minutes, seconds.
Code:
function secondsToString(seconds) {
var numdays = Math.floor(seconds / 86400);
var numhours = Math.floor((seconds % 86400) / 3600);
var numminutes = Math.floor(((seconds % 86400) % 3600) / 60);
var numseconds = ((seconds % 86400) % 3600) % 60;
return numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}
It is tricky to calculate the number of months between two dates as the number of days in each month varies. You may find this useful:-
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DaysWeeksMonths</title>
</head>
<body>
<h2>Enter from and to dates MM/DD/YYYY</h2>
From:<input type="text" id="date1" name="date1" value="1/1/2011"/>
To:<input type="text" id="date2" name="date2" value="1/1/2012" />
<div>How many Days: <span id="outDays"></span></div>
<div>How many Weeks: <span id="outWeeks"></span></div>
<div>How many Months: <span id="outMonths"></span></div>
<div>How many Years: <span id="outYears"></span></div>
<input type="button" onclick="howmanyDWM()" value="GetDate" />
<script type="text/javascript">
function howmanyDWM(){
//Set the two dates
var date_1 = document.getElementById("date1").value;
var date_2 = document.getElementById("date2").value;
//Get 1 day in milliseconds
var one_day = 1000*60*60*24;
//Get 1 week in milliseconds
var one_week = 1000*60*60*24*7;
var one_month = 4.348 * one_week;
var one_year = 1000*60*60*24*365;
date1 = new Date(date_1); //Month is 0-11 in JavaScript
date2 = new Date(date_2);
//Calculate difference btw the two dates, and convert to days
var resultDays = Math.abs(Math.ceil((date2.getTime()-date1.getTime())/(one_day)));
var resultWeeks = Math.abs(Math.floor((date2.getTime()-date1.getTime())/(one_week)));
var resultMonths = Math.abs(Math.round((date2.getTime()-date1.getTime())/(one_month)));
var resultYears = Math.abs(Math.floor((date2.getTime()-date1.getTime())/(one_year)));
document.getElementById("outDays").innerHTML = resultDays;
document.getElementById("outWeeks").innerHTML = resultWeeks;
document.getElementById("outMonths").innerHTML = resultMonths;
document.getElementById("outYears").innerHTML = resultYears;
}
</script>
</body>
</html>
But does anyone really care about their age in hours, minutes, seconds? Or knows the time of their birth that accurately? Obviously the hours, minutes, seconds calculated from midnight on the birthday are not at all an accurate statement - meaningless in fact.. Or is this just a homework exercise?
You can shorten your code to
var mName = ["January","February","March","April","May","June","July","August","September","October","November"," December"];
<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.