Home arrow JavaScript arrow Page 4 - Using the Date() Object to Create a Clock
JAVASCRIPT

Using the Date() Object to Create a Clock


Since the idea of a date and time being a convenient display for computer users was someone's brainchild, programmers have been at the end of their tether trying to standardize it for operating systems like Windows. Apart from the many time zones, it seems that the way in which the date and time are displayed can be enough to irritate a user. In this article you'll learn how to do it without too much trouble thanks to the date() object.

Author Info:
By: Stephen Davies
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
September 10, 2007
TABLE OF CONTENTS:
  1. · Using the Date() Object to Create a Clock
  2. · So, what is the time?
  3. · A Live Clock Example
  4. · A Useful Date Format Example

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials