We can display a clock which will be showing the local time of the client computer by using JavaScript. Note that this time displayed is taken from user computer and not from the server.
Video Tutorial on Real time changing JavaScript Clock
We have seen in our date object example how to display current date and time where the current date and time is displayed once. Here we will try to display changing clock or real time change in date and time by using setTimeout function. This setTimeout function we have used to trigger the time display function in a refresh rate of 1000 mill seconds ( 1 Sec ). This refresh rate can be changed to any other value. By changing the value to 5000 the clock will refresh and show the exact time once in every 5 seconds.
Here is the demo of this script and code is given below that.
Here is the code
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var x = new Date()
document.getElementById('ct').innerHTML = x;
display_c();
}
</script>
</head>
<body onload=display_ct();>
<span id='ct' ></span>
</body>
</html>
Changing the display format
To change the display format we need to change the function part only where we can change the display part.
Let us start with using UTC string
function display_ct() {
var x = new Date()
var x1=x.toUTCString();// changing the display to UTC string
document.getElementById('ct').innerHTML = x1;
tt=display_c();
}
The output is here
Displaying in mm/dd/yy H:m:s format
Here is the code
function display_ct() {
var x = new Date()
var x1=x.getMonth() + 1+ "/" + x.getDate() + "/" + x.getFullYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" + x.getSeconds();
document.getElementById('ct').innerHTML = x1;
display_c();
}
Note that we have only changed the display_ct() function part , other code remains same. Here is the output
Displaying AM or PM with clock
<script>
function display_ct5() {
var x = new Date()
var ampm = x.getHours( ) >= 12 ? ' PM' : ' AM';
var x1=x.getMonth() + 1+ "/" + x.getDate() + "/" + x.getFullYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" + x.getSeconds() + ":" + ampm;
document.getElementById('ct5').innerHTML = x1;
display_c5();
}
function display_c5(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct5()',refresh)
}
display_c5()
</script>
<span id='ct5' style="background-color: #FFFF00"></span>
Displaying 12 hours format
<script>function display_ct6() {
var x = new Date()
var ampm = x.getHours( ) >= 12 ? ' PM' : ' AM';
hours = x.getHours( ) % 12;
hours = hours ? hours : 12;
var x1=x.getMonth() + 1+ "/" + x.getDate() + "/" + x.getFullYear();
x1 = x1 + " - " + hours + ":" + x.getMinutes() + ":" + x.getSeconds() + ":" + ampm;
document.getElementById('ct6').innerHTML = x1;
display_c6();
}
function display_c6(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct6()',refresh)
}
display_c6()
</script>
<span id='ct6' style="background-color: #FFFF00"></span>
Add one leading zero to all single digit in month , date, hour , minute and seconds.
<script>function display_ct7() {
var x = new Date()
var ampm = x.getHours( ) >= 12 ? ' PM' : ' AM';
hours = x.getHours( ) % 12;
hours = hours ? hours : 12;
hours=hours.toString().length==1? 0+hours.toString() : hours;
var minutes=x.getMinutes().toString()
minutes=minutes.length==1 ? 0+minutes : minutes;
var seconds=x.getSeconds().toString()
seconds=seconds.length==1 ? 0+seconds : seconds;
var month=(x.getMonth() +1).toString();
month=month.length==1 ? 0+month : month;
var dt=x.getDate().toString();
dt=dt.length==1 ? 0+dt : dt;
var x1=month + "/" + dt + "/" + x.getFullYear();
x1 = x1 + " - " + hours + ":" + minutes + ":" + seconds + " " + ampm;
document.getElementById('ct7').innerHTML = x1;
display_c7();
}
function display_c7(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct7()',refresh)
}
display_c7()
</script>
<span id='ct7' style="background-color: #FFFF00"></span>
To display clock inside a textbox
Create a textbox and give id to it
<input type=text id='t1' size='70'>
For displaying add this line inside the function display_ct()
We can display 5 hours as 05 hours or 2 minutes as 02 minutes.
// date part ///
var month=x.getMonth()+1;
var day=x.getDate();
var year=x.getFullYear();
if (month <10 ){month='0' + month;}
if (day <10 ){day='0' + day;}
var x3= month+'-'+day+'-'+year;
// time part //
var hour=x.getHours();
var minute=x.getMinutes();
var second=x.getSeconds();
if(hour <10 ){hour='0'+hour;}
if(minute <10 ) {minute='0' + minute; }
if(second<10){second='0' + second;}
var x3 = x3 + ' ' + hour+':'+minute+':'+second
<span id='ct4' style="background-color:#FFFF00"></span>
Displaying Server time using Ajax
This script uses JavaScript to read client computer time and display the same. By using PHP or any other server side script we can display server time. But here we can get once the time that is the time of the page rendered from server. To display updated or real time clock showing server time we have to send request to server periodically. To do this we can use Ajax to get current date and time from server and display in one second interval.
Great tutorial.
Do you know if it would be possible to display the servers time instead of the clients?
smo
14-12-2013
You need to use and server side script like PHP, ASP etc to display server time.
Here is an example of displaying server time using PHP.
Madhu
07-02-2014
wow..it is really helpful. But i want same thing in date picker control. Is it possible ?
ramesh
14-04-2014
i want to change the text font size and font name....How?
smo
14-04-2014
By using style property we can manage font size and font color. Demo added.
nagaraj
15-05-2014
it is help full,
Fahad
19-05-2014
How to change time-zone in example UTC string?
www.ibrahimaziz.biz
23-07-2014
Works like charm !, thank you so much,
greetings from Indonesia :D.
qwho
29-08-2014
wow! my year displaying as 114 instead of 2014 had been bugging me a long time,
went looking today and found this page, thank you!
Neha
24-09-2014
Wow great work.. !
Happy to find this blog..
Thanks sir..
hack
04-10-2014
Fantastic tutorial.........thank you!!!
susan
17-10-2014
I want to start with server time, which works for the rendering time of page. But then stops. How to increment?
I used
var x = new Date(<?php echo time(); ?>*1000)
Any idea?
smo
18-10-2014
To get updated server time , you need to refresh the page at a interval.
Another way is to use Ajax which gets the time from the server without refreshing the page.
susan
18-10-2014
'var x = new Date( < ?php echo time( ); ? > *1000);'
Unfortunately the PHP gets stripped out of my code...
This code initiates 'new Date()' with server date.
I expect your code to increment as it does, when 'new Date()' is initiated with local time.
Why that does not work?
kedar
20-10-2014
i saw your stop watch script how i can run the clock without clicking button,
and i want to show the same time even after refresh the page. How i can do.
smo
20-10-2014
To get the server time and increment it we have to use Ajax.
It connects to server in every one second and gets the data.
You can read the tutorial and download the script here.
mekala
22-12-2014
Really,this date and time function useful for my coding...Thank you
amit verma
13-01-2015
hi there i need to show live clock of to different time zones how can i do so
Heslon
13-01-2015
Thanks so much, I have enjoyed learning through this tutorials.
Christina
17-02-2015
You should consider altering the code for any time
that has a minute or second that is less than 10.
Your way shows the time as being "9:2:3" instead of "9:02:03"
hazel
30-04-2015
it help me alot.. :D
Pandian
04-06-2015
It works and very useful.. Thank you..
ram
23-06-2015
nice one tutorials.
krishan
05-07-2015
Thank you so much.........
Imran Ashraf
08-10-2015
Your code is working correctly. Thanks
Robert Wieneke
23-10-2015
can you display CST and UTC at the same time?
I tried pasting both scripts on the same page but only one will run.
Mazharul Hoque
29-05-2016
Hi Sir,
How can i display it in this format
Date is May 27, 2016
Time is 10:41:02 PM
Thanks,
Mazharul
Aman Bhardwaj
31-08-2016
Hi, I want to check how can we add time for Different countries like, IST EST ,
South Africa or Dubai in a continuous clock program.
Aman
31-08-2016
can you display CST and UTC at the same time?
I tried pasting both scripts on the same page but only one will run.
smo1234
01-09-2016
You can display both.
See that you have one span tag with id ct ,
create one more span tag with id=ct1 , then try to create x1
// changing the display to UTC string
var x1=x.toUTCString();
document.getElementById('ct1').innerHTML = x1;
Now you can display both
smo1234
02-09-2016
One more page added showing time in different cities of different time zone.
For comparison , local time and GMT time is also shown.
Let us know how they are working. More cities will be added to this.
Link is at end of the article.
Peter
26-01-2017
Can I also Display a Real time changing Clock of an internal time Server in a HTML Website?
Gary Y.
24-06-2018
What is the declared variable "strcount" for?
And why have the return value of "tt" for the "display_c()" function call?
Great tutorial!
smo1234
29-06-2018
Both are not required so removed .
Thanks
11-01-2020
This is not useful because if the user changes his or her time setting it will show wrong GMT time. Please provide a solution that can not be tricked by web user.
Thanks for your work anyways.
05-03-2020
For this the time setting of Server side is to be used, so the user can't change the value. The present script depends on user PC time setting so it will read the local time only.
There is a link to Server side Clock.
You can compare the server time and the user time and then allow the time settings.
✖
We use cookies to improve your browsing experience. . Learn more