HTML and CSS Reference
In-Depth Information
theAM_PM = “AM”;
}
else {
theAM_PM = “PM”;
}
if (theHour == 0) {
theHour = 12;
}
if (theHour > 12) {
theHour = theHour - 12;
}
var theMinutes = theDate.getMinutes();
theMinutes = theMinutes + ““;
if (theMinutes < 10) {
theMinutes = “0” + theMinutes;
}
var theFullTime = theHour + “:” + theMinutes + “ “ + theAM_PM;
document.theForm.currentTime.value = theFullTime;
}
//-->
</script>
Essentially, the conditional statements modify the values returned from the JavaScript function calls
to fit the 12-hour clock model. First, the AM or PM suffix is calculated depending on whether the
hour is less than or greater than 12. Next, the hour variable is modified if it is also greater than 12
to use the U.S. rather than the European or military time standard. Finally, if the minutes returned
are under 10, a leading zero is added — something JavaScript does not do. When all the calculations
are finished, the time string is created and placed in the (somewhat disguised) text form field, as
shown in Figure 21-4.
FiGure 21-4
It should be noted that these three methods are not mutually exclusive and can be intermingled easily.
For instance, you could use an onload event handler in the
onload <body> tag to call the getCurrentTime()
function when the page loads as well as call it when the user clicks the button.
There is a fourth method for making JavaScript functions available: Include a
link to an external file. This technique is covered in Lesson 22.
Search WWH ::




Custom Search