Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Getting the time

To get the time in JavaScript you use an instance of the built in Date object. This object has methods that allow you to get the current year, month, day of month, day, hour, minutes and seconds. The methods are given a corresponding name according to what you want to get. For example if you want to get the current second, you use getSecounds().

The following example is going to display digital clock in the middle of the screen. The clock is reference every one second to make it behave like a normal clock. Run Code | View Code

Process

Events

Input

Event Trigger

The onLoad event of the document will call the setInterval method that calls the showCurrentTime function after one second has expired for infinity

The Solution

Define the text field

Define the text field in a form and give it a name <form name="myForm"> <input type="text" name="timeFiled"> </form>

Define the body element

Make the onload event call the setInterval method that will call showCurrentTime every one second <body
onLoad=" setInterval('showCurrentTime()',1000)"
>

Define showCurrentTime

Function analyses

The showCurrentTime function's purpose is to show the current time.

The function is called after one second has expired by the setInterval method of the window object. The calling process is infinitive, it never stops because the setInterval method keeps calling this function after every one second.

The setIntervl method is called by the onLoad event of the document. Therefore as soon as the document loads this function is called and the clock makes it's first tick and comes to life.

Once the function is called it declares five local variables: myDate , seconds, minuets, hours timeStr. MyDate is used to store an instance of the Date object, Seconds, minuets, and hours are used to store the current time and timeStr is used to store a digitized version of the time.

The function then makes a new Date object and calls it myDate. In other words it makes an instance of the Date object and references it to myDate.

myDate = new Date()

After an instance of date is created it then uses it's get methods to extract time segments out of it and place them into the time variables. getSeconds assigns the seconds variable with the current second. getMinutes assigns the minutes variable with the current minute and getHours assigns the hours variable with the current hour.

seconds = myDate.getSeconds() minutes = myDate.getMinutes() hours = myDate.getHours()

The digitizes function is then called and passed hours, minutes, and seconds to get a formatted representation of a digital clock and sets it into timeStr.

timeStr = digitizes(hours, minutes, seconds)

The digitizes 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 minutes. Once it's fomated the digital format is returned.

It then makes the value of the time text field the same value as the digitized format in timeStr.

document.myForm.timeFiled.value = timeStr

Comments with code

function showTime(){
An instance of Date var myDate Current second var seconds Current minute var minuets Current hour var hours Formatted time var timeStr 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 getSecounds 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 digitizes function to format the current seconds,minutes,hours timeStr = digitizes(hours,minutes,seconds) Make the value of the text field the same value as timeStr document.myForm.timeFiled.value = timeStr

Define the digitizes function

When the digitizes function is called and is passed hours, minutes, and seconds it returns a formatted representation of a digital clock.

The digitizes 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 digitalFormat.

function digitizes(seconds,minutes,hours){

This function's argument array var args Digital format representation var digitalFormat Get this function's argument array and set it on it args args = digitizes.arguments Initialize digitalFormat to a 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++){
Is the current argument less then nine if(args[i] <=9){
Append a zero(0) onto the end of digitalFormat digitalFormat += "0"
} 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) }
}

Importent

Run Code | View Code

See also: Making cookies | Using setTimeout


Using alert | Using the status bar | Changing the background color of the document | Changing the content of a document | Using prompt | Using confirm | Changing the URL of a window Manually | Querying a text field's value | Checking the selected value of a select object | Checking if a checkbox is selected | Checking if a radio button is selected in a group | Making a rollover | Preloading images | Using setTimeout/setInterval | Making windows | Making cookies

JavaScript Tutorial Home