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. |
<html> <head> <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)) {
}
}
}
}
}
}
}
} |
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
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.
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. |