JavaScript
  Home arrow JavaScript arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
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 Useful Date Format Example


    (Page 4 of 4 )

    Here is a quick and simple way to just display the date:

    <script type="text/javascript">
     
    document.write(Date())
    zzzzz
    </script>

    That is all very well, as now we have a date displayed. For the final part of this tutorial, however, an example of further formatting will be shown for the date display. First, the date can be further formatted for clarity. For example, rather than display a date as 27-08-07, why not state the month by displaying 27-August-2007. This can be accomplished by using arrays and the getmonth() function.

    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--
     
    var months = new Array("January", "February", "March", "April",
    "May", "June", "July", "August", "September", "October",
    "November", "December");
     
    var today = new Date();
     
    var thedate = today.getDate();
     
    var themonth = today.getMonth();
     
    var theyear = today.getFullYear();
     
    document.write(thedate + "-" + months[themonth] + "-" +
    theyear);
    //-->
    </SCRIPT>

    Script coders will remember the array format and how it is used. In case there is some revision needed here, there is a variable called months used to contain the months of the year. The variable called months is created to store the months of the year in the array constructor. To point at the month names in the array, the code uses getMonth().

    For example, getMonth would return the digit 7 for August. So in the final line where it states months[themonth], for August, themonth=7. The array does its work here by becoming months[7] and that array location points to August. Hold on, that is the number 7. Surely, August is the eighth month of the year! But remember how arrays handle numbers; they begin counting from 0 and not 1.

    This concludes the overview of the date() object and some useful tips on how it can be used. It is always a nice touch to insert a live time display and date on a web page.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - 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






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