Adding the Current Date and Time

Nothing makes your page seem more current than adding the date and the time. While they're a bit more complicated to format in a particular way, just adding them is not difficult at all.

To add the current date and time to your page:


In a separate text document, on the first line, type document.write(


If desired, type "<tag>"+, where tag is the (X)HTML structure you'd like to apply to the date.


Next type Date() to call JavaScript's internal date function.


If you've added an element in step 5, type +"<\/tag>", where tag is the corresponding closing tag. Notice the extra backslash that hides (X)HTML's forward slash / from JavaScript.


Type ) to finish the document.write function (Figure 20.1).

Figure 20.1. I recommend creating your Java-Script scripts in independent, external documents. They should be saved in text-only format with the .js extension. For example, this file is called time.js. Notice that there is no return; it wraps here in order to fit in this narrow width column.


Save the document as in text-only format, using the file name time.js.


In your (X)HTML document, place the cursor where the time should appear.


Type <script type="text/javascript" language="javascript" src="/books/2/62/1/html/2/time.js"> </script> to call the script from that location (Figure 20.2).

Figure 20.2. Put the script element in the place in your (X)HTML document where you'd like the time to appear. Note that the language attribute is deprecated and thus only valid in (X)HTML transitional (see page 313).

Figure 20.3. A time stamp on your page makes it appear more current. You just have to make sure the content is up to date as well.


  • When you know more JavaScript, you can format the date, add the full name of the week and month, and change the order of the elements to better suit the situation at hand.

HTML, XHTML, & CSS(c) Visual QuickStart Guide
HTML, XHTML, and CSS, Sixth Edition
ISBN: 0321430840
EAN: 2147483647
Year: 2004
Pages: 340

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: