filmov
tv
JavaScript real time clock showing change in date and time in different formats

Показать описание
We will divided this in two parts , in part one we will understand how to display the present date and time by clicking a button. Every time we click the button the current date and time is displayed.
In part two we will not use the button but connect it to a timer function so at every one second interval the current date and time is displayed. So it will show the continuous clock or real time clock in our screen.
Note that we are using client side time to display and this not the server time.
Create a function display_ct() to display the date and time.
In this function there are mainly two lines. The first line reads the current date time from client computer and store it in a variable x .
The second line assign the value of x to one SPAN element or DIV element having ID = ct . So what ever the value of x it is displayed inside the element having id = ct.
We will create one button and on click event of this button we will connect to this function. When ever we click the button this function executes and displays the time inside the element having id=ct.
In part two we will not use any Button click to trigger our display function. We will use one timer function to execute our display_ct() function at an interval of one second. Out timer function setTime() takes two inputs , one is the function it will execute and other one is the time gap in milli seconds. Here we are using 1000 milli seconds or 1 second as time interval. After this time interval our timer will trigger the our display function display_ct(). We will use the function display_c() keep time function inside it.
Repeating the timer function
Now we have two functions, one is display_ct() to display the date and time. Another function is display_c which will store the timer to trigger the display_ct() function after a time gap of one second. To make this process repetitive we will call display_c() function from display_ct() function. By doing this we will create one loop where both functions will execute each other and calling of display_ct() from display_c() is through the timer setTime() with a delay of one second.
Each time the display_ct() is executed , the function will display the current date and time.
In part two we will not use the button but connect it to a timer function so at every one second interval the current date and time is displayed. So it will show the continuous clock or real time clock in our screen.
Note that we are using client side time to display and this not the server time.
Create a function display_ct() to display the date and time.
In this function there are mainly two lines. The first line reads the current date time from client computer and store it in a variable x .
The second line assign the value of x to one SPAN element or DIV element having ID = ct . So what ever the value of x it is displayed inside the element having id = ct.
We will create one button and on click event of this button we will connect to this function. When ever we click the button this function executes and displays the time inside the element having id=ct.
In part two we will not use any Button click to trigger our display function. We will use one timer function to execute our display_ct() function at an interval of one second. Out timer function setTime() takes two inputs , one is the function it will execute and other one is the time gap in milli seconds. Here we are using 1000 milli seconds or 1 second as time interval. After this time interval our timer will trigger the our display function display_ct(). We will use the function display_c() keep time function inside it.
Repeating the timer function
Now we have two functions, one is display_ct() to display the date and time. Another function is display_c which will store the timer to trigger the display_ct() function after a time gap of one second. To make this process repetitive we will call display_c() function from display_ct() function. By doing this we will create one loop where both functions will execute each other and calling of display_ct() from display_c() is through the timer setTime() with a delay of one second.
Each time the display_ct() is executed , the function will display the current date and time.
Комментарии