How to Add a Clock to a Web Page

Save

JavaScript is a scripting language that enables you to add dynamic features to a web page, including the current time. You can then format a clock on your page and embed the JavaScript code within hypertext markup language (HTML). You’ll need to have access to the actual HTML code of your page to insert the code. You can customize the clock’s JavaScript code for your particular project.

  • Open the HTML page where the clock will be inserted in a text editor of your choice by selecting \"File\" and \"Open\" from the toolbar.

  • Copy the following JavaScript code (originally derived from QuackIt and then customized), then paste it between the <HEAD> tag of your text editor.

    <script type=\"text/javascript\" language=\"JavaScript\">
    <!--
    function getCurrentTime()
    {
    var now = new Date()
    GO
    var h = now.getHours()
    GO
    var m = now.getMinutes()
    GO
    var pm = 'PM'
    GO
    var am = 'AM'
    GO

    if (m < 10)
    m = '0' + m
    if (h >= 12)
    m = m + pm
    if (h < 12)
    m = m = am

    var time = h + ':' + m
    GO
    return time
    GO
    }
    //-->
    </script>

  • Copy the following code, then paste it between the <BODY> tag, where you want the clock to appear:

    <script type=\"text/javascript\" language=\"JavaScript\"><!--
    var myClock = getCurrentTime()
    GO
    document.write(\"The time is \" + myClock + \".\")
    GO
    //--></script>

  • Select \"File\" and \"Save\" from the toolbar to save your work. Press \"Save\" in the \"Save\" dialog box.

References

  • Photo Credit digital clock image by JoLin from Fotolia.com
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!