|
|
|
<head>
<script language="javascript">
function showTime(){
/*
Declare local variables
*/
var myDate //An instance of Date
var seconds//Current second
var minuets//Current minute
var hours//Current hour
var timeStr//Formatted time
/*
Create an instance of the built in Date object and
assign this instance of the Date object to myDate variable.
*/
myDate = new Date()
/*
Use the getSeconds method of myDate instance to get the current second
and insert the returned value into the seconds variable
*/
seconds = myDate.getSeconds()
/*
Use the getMinutes method of myDate instance to get the current minute
and insert the returned value into the minutes variable
*/
minutes = myDate.getMinutes()
/*
Use the getHours method of myDate instance to get the current hour
and insert the returned value into the hours variable
*/
hours = myDate.getHours()
/*
Call the digitized function to format the current seconds,minutes,hours
*/
timeStr = digitized(hours,minutes,seconds)
/*
Make the value of the text field the same value as timeStr
*/
document.myForm.timeFiled.value = timeStr
/*
Define the digitized function
*/
function digitized(seconds,minutes,hours){
/*
When the digitized function is called and is
passed hours, minutes, and seconds it returns a
formatted representation of a digital clock.
The digitized function does it's job by formatting the given
hours, minutes and seconds. If hours, minutes or seconds are
less then nine it inserts a zero (0) before the hours, minutes or
seconds it also puts a colon (:) after the hours
and after the minutes. Once it's formatted digital Format it
returns the formatted digital Format.
*/
/*
Declare local variables
*/
var args //This function's argument array
var digitalFormat// Digital format representation
/*
Get this function's argument array and set it on args
*/
args = digitized.arguments
/*
Initialize digitalFormat to an empty-string since the loop uses += operator
*/
digitalFormat = ""
/*
Scan through every time segment and check if it is less then nine,
if it is insert a zero(0) before the segment.
Also insert a colon (:) after each segment
*/
for(var i=0;i<=args.length-1; i++){
if(args[i] <=9){ //Is the current argument less then nine
digitalFormat += "0" // Append a zero(0) onto the end of digitalFormat
}
/*
Append the current argument and a colon (:) to digitalFormat
*/
digitalFormat += args[i] + ":"
}
/*
Remove the last colon (:) at the end of digitalFormat and return digitalFormat
*/
return digitalFormat.substring(0,digitalFormat.length-1)
}
}
}
</script>
</head>
<!--
call showCurrentTime every one second <body
onLoad="
setInterval('showCurrentTime()',1000)"
>
<form><!-- Open the form -->
<!--
Define the text field in a form and give it a name
-->
<input type="text" name="timeFiled">
</form><!-- Close the form -->
</body><!-- Close the body element -->