JavaScript
  Home arrow JavaScript arrow Page 3 - Using the Date() Object to Create a Clock
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Using the Date() Object to Create a Clock
By: Stephen Davies
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-09-10

    Table of Contents:
  • Using the Date() Object to Create a Clock
  • So, what is the time?
  • A Live Clock Example
  • A Useful Date Format Example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using the Date() Object to Create a Clock - A Live Clock Example


    (Page 3 of 4 )

    In this section, the article shall outline how to display the time on a web page while the next section shows some formats that can be achieved.

    Today some formatting principles are needed. Where clocks are concerned, the recognized format is hours, minutes, seconds, like so: 21:38:45. So here is the code:

    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--
    function liveclock(){
     
    var datetoday = new Date();
     
    var thehour = datetoday.getHours();
     
    var themin = datetoday.getMinutes();
     
    var thesec = datetoday.getSeconds();
     
    var livetime = thehour + ":" + themin + ":" + thesec;
     
    document.clock.showtime.value = livetime;
    }
    //-->
    </SCRIPT>

    We will get to the form part in a second. This will now give the time a recognizable format for any viewer of the web page. A clock and a date are nice real time additions to any web page. The format will show as:

    09:10:35

    Of course, we realize quite quickly that there is a static clock on the screen. That's not really an effective "live" clock as described! Not to worry, as there is a method called setInterval() which will solve this problem. The solution is not as complex as it may seem. Most coders are familiar with loops in code. Well, in a similar fashion, the code here needs to call the function liveclock() every second so that it continuously updates by the second like a live clock. Since there are 1000 milliseconds in a second, setInterval() is set to that value.

    Now, a line of code has already dealt with placing the time on the form text box. There is the name clock which is the name of the form, and showtime which is the name of the text box.

    document.clock.showtime.value = livetime;

    Using basic form hierarchy, the value in livetime is placed inside the text box.

    In the HTML code of the page, decide where the clock is to be placed and insert:

    <FORM NAME="clock">
     
    <INPUT TYPE="TEXT" SIZE="11" MAXLENGTH="12"VALUE="" NAME="showtime">
    </FORM>
    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--
     
    setInterval("liveclock()", 1000);
    //-->
    </SCRIPT>

    Now we will look at that more closely and break down the logic applied here.

    1. An instance of the Date() object called datetoday is created and assigned that value to the variables thehour, themin, thesec and livetime. Feel free to change the variable names for the instances as you please. This is just an example.
    2. Next, using basic form hierarchy, the time was displayed in a standard text box by placing the livetime digits into the text box value.
    3. Then, the interval was required to run a loop that would refresh the clock time every 1000 milliseconds or second.

    Where milliseconds are concerned a date object can be written as:

    Today = new Date (.....) - as in new Date(milliseconds);

    Using Date.parse(), it is possible to display the number of milliseconds of a Date string. Here is a format example with the returned value calculated and placed in Today.

    Today = new Date("month date, year");

    More JavaScript Articles
    More By Stephen Davies


       · Hi There,Thanks for the nice article about placing the date and time on my...
       · You are welcome. i will be posting more javascript articles on various useful...
     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT