Create a Simple Javascript Clock

When you think “time” for a website, you automatically think “flash powered visual phenomenon,” at least i do. If you take a primary example, on the BBC website, in the header on the right, a flash powered clock. What if you have no flash player installed (i know, that’s a rareity nowadays) but, it can happen, you would be left with a big ugly, “install the latest flash player” button. To avoid this, for a client, i inserted a Javascript clock into his website, now you might say: “what if they have JS disabled?” Well, it’s more likely that they won’t go through the trouble of manually disabling Javascript in modern day browsers.

Ok, before anything, this isn’t styled, so you can just let rip with CSS as you wish :D
First, we have to add the Javascript:

<script type=”text/javascript”>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById(‘txt’).innerHTML=h+”:”+m+”:”+s;
t=setTimeout(’startTime()’,500);
}
function checkTime(i)
{
if (i<10)
{
i=”0″ + i;
}
return i;
}
</script>

With JS we’re creating the div of “txt” to show the time. Now all we have to is add the HTML:

<body onload=”startTime()”>
<div id=”txt”></div>

We’ve specified the txt div, and the time should now show.

It’s a simple, quick and effective way of showing the time, have fun ;) Check the demo for the end product.

View Demo

We offer testking 646-671 web design video tutorial for Beginners. Learn modern web designing using testking 642-691 guide and testking 642-262 live demos.

Written by Ben Bate

I'm Ben, a freelance designer from Plymouth, England specialising in UI design. I'm the director of iPhone app development company, PixelBit Apps Ltd and also run Pixel Clouds design blog.

28 Responses to “Create a Simple Javascript Clock”

  1. Larry Battle says:

    Nice script. I changed it a bit to the following.

    function checkTime(i) {
    return ( i < 10 ) ? '0' + i : i;
    }
    function updateTime( id ) {
    var today = new Date(),
    h = today.getHours(),
    m = checkTime(today.getMinutes()),
    s = checkTime(today.getSeconds());

    window.document.getElementById( id ).innerHTML = h + ":" + m + ":" + s;
    }
    function startTime( id ){
    t = window.setInterval( function(){ updateTime( id ); }, 500);
    }

  2. imon says:

    Great work, nice and short just as I like it

  3. LEVESQUE says:

    I really enjoyed this posting! If you are interested in any web design feel free to contact me at http://www.helixfire.com

  4. Did you create your own blog or did a program do it? Could you please respond? 73

  5. admin says:

    Created it myself =]

  6. very cool & good post, thank you very much for sharing. But

    Not Found

    The requested URL /demos/jsclock.html was not found on this server.

    Can you share this js code on my JavaScript library?

    Awaiting your response. Thank

  7. DyDesign says:

    This is very neat and detailed. Expect more from you! :D

  8. arjun says:

    @JavaScriptBank.com:

    I’ll get that back up as soon as possible :D

  9. free articles for your ezine says:

    wow…. amazing information…

  10. free internet tv says:

    awesome thanks!.

  11. arjun says:

    @javascriptbank, yes you can ;)

  12. You made some good points there. I did a search on the topic and found most people will agree with your blog. Can I subscribe to your site?

  13. admin says:

    Yep sure. The RSS feed button is in the sidebar. Cheers, Ben

  14. Hi, I’ve been a lurker around your blog for a few months. I love this article and your entire site! Looking forward to reading more!

  15. I just found this website a while ago when a good friend suggested it to me. I’ve been a regular reader ever since.

  16. I came recently to your blog and have been reading along. I thought I would leave my initial comment. Keep posting, cause your posts are wonderful! Doesn’t it take up a lot of time to keep your website so fascinating ?

  17. RSS feed is not working in chrome, Kindly fix it.

  18. thanks !! very helpful post!

  19. Hi, I can’t understand how to add your site in my rss reader. Can you Help me, please :)

  20. admin says:

    Just click on the RSS button in the sidebar. Its easy from there =] Cheers, Ben

  21. Jonh says:

    THanks very muck!

  22. Thank you for a great post

  23. Great tips I will be trying that tonight

    thanks

    Roger

  24. Thanks for posting the info I might try that on my website this weekend.

    Regards

    Geoff

  25. Really great pictures on this page.

  26. Jam says:

    Nice post, was wondering if you would allow me to link to it in a blog post i am currently writing for my own website? Cheers Jam

  27. admin says:

    Sure, go ahead :)

  28. Jim says:

    Great tutorial! Nice and short. But the link to your demo does not work? :s

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>