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

preview_player
Показать описание
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.
Рекомендации по теме
Комментарии
Автор

Sir Mindblowing It helped me Thank you so much

yashkapure
Автор

Came from the webpage to provide feedback. Using the code from the webpage linked in the Description, I was able to get the current time displaying on my webpage! It worked great for me! Thank you!

alexbarr
Автор

I made one analog and beside of the one drop box that box shows the different countries names. So my question is how to connect that analog clock with that drop box means If I am clicking any of the country which is already had in drop box and than that country time have to show on the analog clock..

itssriya
Автор

Sir how we can convert the analog clock seconds into values

owaisahmed
Автор

Anyone has an idea how to do this without <body onload=display_ct();> ?
thanks!

djwadl
Автор

Hello, I have a question. I'm setting up a webserver on my local server and I would like to put a clock that display my local time from my server. So if anyone visits the page they will only see my time only and not theres. How can I do that?

DiyintheGhetto
Автор

how do you change it to an AM/PM format? aka 12 hr clock?

aingeav
welcome to shbcf.ru