PDA

View Full Version : Dating problems:

thenextbesthang
12-08-2006, 05:58 PM
Here's the deal. I tell the user to input 2 dates, giving me the year, month, day, hour, minute, and second. I need to find the time that has elapsed between the two dates. Here is the code:

<html>
<title>CSC Lab 20</title>
<script type = "text/javascript">
function gettime()
{
var orig = document.getElementById("original");
var nw = document.getElementById("new");
var output = document.getElementById("mydiv");
var msy = 365*30*24*60*60*1000;//year in ms
var msm = 30*24*60*60*1000;//month in ms
var msd = 24*60*60*1000;//day in ms
var msh = 60*60*1000;//hour in ms
var msmn = 60*1000;//minute in ms
var mss = 1000;//second in ms
var oyims = orig.year1.value*msy;
var omims = (orig.month1.value*msm);
var odims = (orig.day1.value*msd);
var ohims = (orig.hour1.value*msh);
var omnims = (orig.minute1.value*msmn);
var osims = (orig.second1.value*mss);
var nyims = nw.year2.value*msy;
var nmims = (nw.month2.value*msm);
var ndims = (nw.day2.value*msd);
var nhims = (nw.hour2.value*msh);
var nmnims = (nw.minute2.value*msmn);
var nsims = (nw.second2.value*mss);
var nms = (nyims+nmims+ndims+nhims+nmnims+nsims);
var oms = (oyims+omims+odims+ohims+omnims+osims);
if(nms > oms)
{
diffinms = nms-oms;
}
else
{
diffinms = oms-nms;
}
output.innerHTML += "Milliseconds: " + diffinms + "<br>";
var eyims = Math.floor(diffinms / (msy));
diffinms = diffinms % (msy);
output.innerHTML += "Years: " + eyims + "<br>";

output.innerHTML += "Milliseconds: " + diffinms + "<br>";
var emims = Math.floor(diffinms / msm);
diffinms = diffinms % msm;
output.innerHTML += "Months: " + emims + "<br>";

output.innerHTML += "Milliseconds: " + diffinms + "<br>";
var edims = Math.floor(diffinms / msd);
diffinms = diffinms % msd;
output.innerHTML += "Days: " + edims + "<br>";

output.innerHTML += "Milliseconds: " + diffinms + "<br>";
var ehims = Math.floor(diffinms / msh);
diffinms = diffinms % msh;
output.innerHTML += "Hours: " + ehims + "<br>";

output.innerHTML += "Milliseconds: " + diffinms + "<br>";
var emnims = Math.floor(diffinms / msmn);
diffinms = diffinms % msmn;
output.innerHTML += "Minutes: " + emnims + "<br>";

output.innerHTML += "Milliseconds: " + diffinms + "<br>";
var esims = Math.floor(diffinms / mss);
diffinms = diffinms % mss;
output.innerHTML += "Seconds: " + esims + "<br>";
}
</script>
<body>
<form id = "original">
Original-Year:<input type = "text" id = "year1" value ="2000" name = "1year"><br>
Original-Month:<input type = "text" id = "month1"value ="1" name = "1month"><br>
Original-Day:<input type = "text" id = "day1" value ="10"name = "1day"><br>
Original-Hour:<input type = "text" id = "hour1" value ="20"name = "1hour"><br>
Original-Minute:<input type = "text" id = "minute1"value ="30" name = "1minute"><br>
Original-Second:<input type = "text" id = "second1"value ="30" name = "1second"><br><br>
</form>
<form id = "new">
New-Year:<input type = "text" id = "year2"value="2005" name = "2year"><br>
New-Month:<input type = "text" id = "month2"value ="11" name = "2month"><br>
New-Day:<input type = "text" id = "day2"value ="25" name = "2day"><br>
New-Hour:<input type = "text" id = "hour2"value ="5" name = "2hour"><br>
New-Minute:<input type = "text" id = "minute2"value ="20" name = "2minute"><br>
New-Second:<input type = "text" id = "second2"value ="20" name = "2second"><br><br>
</form>
<input type = "button" name = "mybtn" id = "btn1" value = "Get Time Difference" onclick = "gettime()"><br><br>
<div id = "mydiv"></div>
</body>
</html>

Essentially, here's what I do:
1) convert both dates into milliseconds
2) Find the difference between the larger amount of milliseconds and the smaller amount of miliseconds
3) Divide that amount by 365, find the remainder - > this is to find the years
4) Divide the new amount by 30, find the remainder -> this is to find the months
5) Divide the new amount by 24, find the remainder -> this is to find the days
6) Divide the new amount by 60, find the remainder -> this is to find the hours
7) Divide the new amount by 60, find the remainder -> this is to find the minutes
8) Divide the new amount by 1000, find the remainder -> this is to find the seconds.

The problem is, with the initial values that I have in there, if on the form 'original', I make the year 2010, it gives me a wrong number for the amount of months that have elapsed. So I changed it - but with the changed code, if I put it back to year 2000, I get a wrong number again! Any help?