[IMAGE] IU SLIS logo

Demo page logo


Javascript

Here is a script that will place the current date on the page

This script makes use of a javascript object callled an array. An array is used to group related elements such as images, or, in this case days and months. Two arrays are set up here.

The variable new Date is then used to select the correct element from the arrays and write them to the page.

The three functions now.getDay, now.getMonth, and now.getFullYear do the work in this script

Notice the use of the javascript comments to explain some parts of the script:

Here is the script that makes it work:

<script language="Javascript">
<!--

// Array of day names
var dayNames = new Array(
"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

// Array of month Names
var monthNames = new Array(
"January","February","March","April","May","June","July","August","September","October","November","December");

var now = new Date();
document.write(dayNames[now.getDay()] + ", " + monthNames[now.getMonth()] + " " + now.getDate() + "," + now.getFullYear());

// -->
</script>

And here's what it looks like when it's used inside a table (the cell background color is black and the font color is white):

Return to Javascript for home use.


Page by Howard Rosenbaum
Find me at hrosenba@indiana.edu http://www.slis.indiana.edu/hrosenba/www/Demo/date.html