HTML and CSS Reference
In-Depth Information
var theHour = theDate.getHours();
if (theHour < 12) {
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>
To convert this code to an external JavaScript file, simply cut only the function code and paste it in a
blank text document, like this:
function getCurrentTime() {
var theAM_PM;
var theDate = new Date();
var theHour = theDate.getHours();
if (theHour < 12) {
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;
}
It is traditional to save the file with a .js extension so that it can be easily identified as a JavaScript
document. Web designers often store their JavaScript files in a site root folder called scripts for
convenience.
Search WWH ::




Custom Search