Time Since and Remaining


More Timing Issues.  Now we want to work with timing events some more to deal with determining something like how many days:hours:minutes:seconds it has been since the semester started and how many days:hours:minutes:seconds it remain until the semester ends.

This is essentially equivalent to Thau's developments for the NASA clocks that relate to how long the space station has been in orbit and how long until the next mission is launched.

What makes this problem interesting, in addition to what we have previously developed, is the necessity of converting milliseconds into actual days, hours, minutes and seconds and then displaying them.

The problem is started by setting the start and end of the semester

semester_start = Date.parse("Monday 30 Aug 2004 08:00:00 EST");

and the end of the semester.

semester_end = Date.parse("Friday 17 Dec 2004 17:00:00 EST");

Then you obtain the current time and date and determine how many milliseconds have elapsed since the beginning of the semester and how many milliseconds remain until the end of the semester.  This needs to be done in milliseconds since it is the most robust measure.  Then these durations need to be converted back into days, hours, minutes and seconds.  This leads to some interesting and worthwhile problem solving.

The following code should be copied into a file called TimeInClass.html.



<title>How Long Must This Go On?</title>

<script language="JavaScript">
function setNewTime()
// initializing dates
now = new Date();
semester_start = Date.parse("Monday 30 Aug 2004 08:00:00 EST");
semester_end = Date.parse("Friday 17 Dec 2004 17:00:00 EST");
timeNow = now.getTime();

// determining remaining and continuing durations
timeLeft = semester_end - timeNow;
timeSince = timeNow - semester_start;

// determining units of time since semester started
// segment for the days since

daysSince = parseInt(timeSince / 86400000);
if (isNaN(daysSince))

daysSince = 0;

// segment for the hours since
timeSince = parseInt(timeSince % 86400000);
hoursSince = parseInt(timeSince / 3600000);
if (isNaN(hoursSince))

hoursSince = 0;

// segment for the minutes
timeSince = parseInt(timeSince % 3600000);
minutesSince = parseInt(timeSince / 60000);
if (isNaN(minutesSince))

minutesSince = 0;

// segment for the seconds
timeSince = parseInt(timeSince % 60000);
secondsSince = parseInt(timeSince / 1000);
if (isNaN(secondsSince))

secondsSince = 0;


// outputting these results to the form
window.document.frmCountDown.txtBeenDays.value = daysSince;
window.document.frmCountDown.txtBeenHours.value = hoursSince;
window.document.frmCountDown.txtBeenMinutes.value = minutesSince;
window.document.frmCountDown.txtBeenSeconds.value = secondsSince;

// determining units of time left in the semester 
// segment for the days left

daysLeft = parseInt(timeLeft / 86400000);
if (isNaN(daysLeft))

daysLeft = 0;

// segment for the hours Left
timeLeft = parseInt(timeLeft % 86400000);
hoursLeft = parseInt(timeLeft / 3600000);
if (isNaN(hoursLeft))

hoursLeft = 0;

// segment for the minutes
timeLeft = parseInt(timeLeft % 3600000);
minutesLeft = parseInt(timeLeft / 60000);
if (isNaN(minutesLeft))

minutesLeft = 0;

// segment for the seconds
timeLeft = parseInt(timeLeft % 60000);
secondsLeft = parseInt(timeLeft / 1000);
if (isNaN(secondsLeft))

secondsLeft = 0;


// outputting these results to the form
window.document.frmCountDown.txtRemainDays.value = daysLeft;
window.document.frmCountDown.txtRemainHours.value = hoursLeft;
window.document.frmCountDown.txtRemainMinutes.value = minutesLeft;
window.document.frmCountDown.txtRemainSeconds.value = secondsLeft;


<body bgcolor="#000048" text="#c0c0c0">

<p><b><font size="5" color="#c0c0c0">How much longer will this class go on?</font></b></p>
<form name="frmCountDown">

<table border="0" cellpadding="4" cellspacing="0" width="901">
<td width="112"><font size="4">It has been</font></td>
<td width="119"><font size="4"><input name="txtBeenDays" size="4" >days</font></td>
<td width="136"><font size="4"><input name="txtBeenHours" size="3" >hours</font></td>
<td width="137"><font size="4"><input name="txtBeenMinutes" size="3" >minutes</font></td>
<td width="347"><font size="4"><input name="txtBeenSeconds" size="3" >
seconds since this semester started</font></td>
<td colspan="5" width="889"><font size="4">&nbsp;</font></td>

<td width="112"><font size="4">There remain</font></td>
<td width="119"><font size="4"><input name="txtRemainDays" size="4" >days</font></td>
<td width="136"><font size="4"><input name="txtRemainHours" size="3" >hours</font></td>
<td width="137"><font size="4"><input name="txtRemainMinutes" size="3" >minutes</font></td>
<td width="347"><font size="4"><input name="txtRemainSeconds" size="3" >
seconds until this semester is over</font></td>
<td colspan="5" width="889"><font size="4">&nbsp;</font></td>

<td colspan="5" align="center" width="889">
<input type="button" value="Find Time" onClick="setNewTime();">





When this is uploaded you should get something like the following.



Remember, sine everything is in milliseconds, all of the conversion factors are 1000 times larger than you would normally expect to see.   The conversion factors are in the following table.


Time Unit Conversion
seconds 1000 milliseconds/second
minutes 60000 milliseconds/minute
hours 3600000 milliseconds/hour
days 86400000 milliseconds/day


So to get each of these amounts within the milliseconds since or remaining you need to divide it by the appropriate conversion factor.  For example, to determine time remaining
  • first divide out the timeLeft by 86400000 to get how many days there are left
    • then modulo timeLeft by 86400000 to get how many milliseconds are left that aren't accounted for by days
  • then divide out this remaining timeLeft by 3600000 to get how many hours that remain in the semester
    • then modulo timeLeft by 3600000 to get how many milliseconds aren't accounted for by hours
  • then divide this timeLeft by 60000 to get how many minutes remain in the semester
    • then modulo timeLeft by 60000 to get how many milliseconds aren't accounted for by minutes
  • then divide this timeLeft by 1000 to get how many seconds remain in the semester

It is important to do this in this order so that you do not do something analogous to giving someone $0.90 change in 18 nickels. 

  • You would want to first figure out how many quarters you should give them which is 90/25 = 3 quarters
  • then modulo 90 % 25 = 15 to see how many cents you have left
  • then divide 15/10 = 1 dime to get how many dimes you should give them.
  • then modulo 15 % 10 = 5 to get how much more change you need to give them.
  • then divide 5/5 = 1 to determine they need a nickel

Thus there change would be 3 quarters, 1 dime and 1 nickel.  This needs to be done in this sequence from greater parts to smaller parts.

Most of the rest of the processing has to do with placing the output into the form.