Current Time

The following code can be placed anywhere in the html file:


// This script will print the current time when the page is loaded
var dateTime = new Date();
var hours = dateTime.getHours();
var minutes = dateTime.getMinutes();
document.write ("<p>The time is: " + hours + ":" + minutes + "</p>");


The script above writes the current time when the page is loaded. The script runs only once on page load, so the time is only updated when the browser is refreshed.

The script first creates a new Date object and assigns it to a variable called dateTime. It then uses the information stored in dateTime to get the values for hours and minutes. Each is also assigned to a variable

Finally, the script compiles a string formed of the variable values and some html, which it writes to the page using the document.write method.