Lesson Code: http://www.developphp.com/video/JavaScript/Digital-Clock-Tutorial-Custom-Animated-Clock
Learn some excellent Javascript fundamentals by working with Javascript functions, variables, the Date object, a looped timer, communicating with page elements in real time, and more to create a custom animated digital clock that will run in all major browser software and on smart phone browsers.
Original source
42 responses to “Javascript Digital Clock Tutorial CSS Custom Animated Clock on Your Website”
Mr. Khoury . . . THANK YOU for this tutorial . . . I finally get it. I have developed a Digital Clock for my Website, and I am wanted it to fit into the design of the Website, and add more "get functions. There was a sheet that was displayed for a moment on your website of Java function or code, but I didn't get where to find that sheet. Would you mind forwarding that information at: rein.wpstudio@gmail.com. I would truly appreciate the assist as I have been attempting to get my TIME in THE LAND OF ENCHANTMENT clock for over 7 years.
Sincerely your,
Rein Whitt-Pritchette
Artist || Serigrapher
Excellent tutorial, easy to follow!!
how can i print same time in a textbox
This tutorial was very good. I can understand each and every part of the structure for the JavaScript and was able to build a clock, thank you.
Can anyone tell me, which timezone do I get from the Date() class..??
Can anyone tell me, which timezone do I get from the Date() class..??
try using setInterval instead
The clock shows a zero before the minutes… Help please?
I was able to figure it out and i fix my problem. So ignore my previous comment the setTimeout("renderTime()",1000) needed to be below the var s = currentTime.getSeconds();
This is my code why doesn't it work for me? on the developer tools it shows this message Uncaught TypeError: Cannot set property 'textContent' of null. Can anybody help?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.clockStyle {
background-color: #000;
border: #999 2px inset;
padding: 8px;
color: #0FF;
font-family: "Arial Black", Gadget, sans-serif;
font-size: 16px;
font-weight: bold;
letter-spacing: 2px;
display: inline;
float: right;
}
</style>
</head>
<body>
<div id"clockDisplay" class="clockStyle"></div>
<script type="text/javascript" language="javascript">
function renderTime() {
var currentTime = new Date();
var diem = "AM";
var h = currentTime.getHours();
var m = currentTime.getMinutes();
var s = currentTime.getSeconds();
if (h == 0) {
h = 12;
} else if (h > 12) {
h = h – 12;
diem = "PM";
}
if (h < 10) {
h = "0" + h;
}
if (m < 10) {
m = "0" + m;
}
if (s < 10) {
s = "0" + s;
}
var myClock = document.getElementById("clockDisplay");
myClock.textContent = h + ":" + m + ":" + s + "" + diem;
myClock.innerText = h + ":" + m + ":" + s + "" + diem;
setTimeout("renderTime()",1000);
}
renderTime();
</script>
</body>
</html>
This is my code and never worked for me
i think i checked many times as my teacher Adam Khoury told me to, but i cannot figure it out where is the problem
a programmer can help me out please?
<!DOCTYPE html>
<html>
<head>
<title>Digital clock</title>
<style type="text/css">
.clockStyle{
background-color:#000000;
border:#41FF41 2px inset;
padding:6px;
color:#66FFFF;
font-size:12px;
font-family:"Arial Black";
letter-spacing: 2px;
display:inline;
}
</style>
</head>
<body>
<div id="clockDisplay" class ="clockStyle"></div>
<script type"text/javascript" language="javascript">
function renderTime(){
var currentTime = new Date();
var diem = "AM";
var h = currentTime.getHours();
var m = currentTime.getMinutes();
var s = currentTime.getSeconds();
if (h == 0){
h = 12;
} elseif (h > 12){
h = h – 12;
diem = "PM";
}
if (h < 10){
h = "0" + h;
}
if ( m <10){
m = "0" + m;
}
if (s < 10){
s = "0" + s;
}
var myClock = document.getElementById("clockDisplay");
myClock.textContent = h + ":"+ m +":"+ s +" "+ diem;
myClock.innerHTML = h + ":"+ m +":"+ s +" "+ diem;
setTimeout("renderTime()" ,1000);
}
renderTime();
</script>
</body>
</html>
Adam,
Thanks for sharing.
great !!!!
Can anybody explain the first part of this statement?
If (h==0) {
h = 12;
}
Hello Adam, these are awesome videos, I am learning alot, I do have some questions and I was wondering if I could contact you sometime and ask you, I am building a membership website which I want to have a certain page reset at midnight for the next day, it is a site for people to add text, and i want the text to delete at midnight is what I mean and have the page start the new day with a blank slate. Please let me know if I can contact you or if you can direct me to one of your videos explaining how to do that.
Thank you for the help man! This works very well for me
Adam, you're awesome! Thanks and looking forward to see more javascript videos!
it appears AM when its PM … idk why someone explain what Adam did wrong
Adam, I'm using a MacBook air. And when I run my click my safari freezes. What is that? Maybe I need to use
WebKit?Thank you very much .. very good video
Thank you so much for this!
The 'language' attribute is deprecated and should no longer be used.
Passing a string to setTimeout is a bad practice.
Good tutorial, liked it.
I love your video ,….
You are the best 😀
I try this method toLocaleTimeString() instead of getting hours,mins and secs.
super!!!! thank you!
awesome 😀
I was looking for some real tutorials and i found a Master…
Code works fine in IE9
not working
Maybe He Paused the Vid?
Thank you very much!
The code doesn't appear to toggle AM and PM.
you default diem to AM.
if h>12 : then you change diem to PM which is good.
however, if the time goes from11:59 to 00:00, diem will remain PM
because you don't have logic to change it back.
you need to add an else to say well if i don't need to make diem PM, change it back to AM
Awesome tutorial, Thanks for the brain food.
what if i want to create an external js file, how do i reference it in the html?
Did that digital clock do a skip at 0:50 ?? It looks like it skipped from "03:10:44 PM" to "03:10:46 PM"… Was that video editing of the tutorial or was that the actual digital clock tweaking?
thanks you are a wonderfull teacher
I would like to find a way to have this clock display GMT no matter where in the world you are viewing the website. is this possible?
i like it but mine is not working please help!
I feel so guilty having not donated. There are very few good teachers like Adams.Thank you Sir!
I'm a very, very new programmer and my biggest problem is understanding the entire scope of programming a particular object to do what you want it to do. What I mean by that is adding the right set of codes to make it work; I simply don't know where to start and how to end it. This is very frustrating for me because I feel like there is no order to learning programming, where as html is easier to learn cause there seems to be order to it. Like in html you know that "oh, this goes here."
@flashbuilding U R A LIFE SAVER!! 😀