Script Tip 31

by Joe Burns, Ph.D.


Never Trust a Tip Over 30!

     Once again, we're working on a requested script here. A reader wanted to post a date that goes beyond the basic number returns. He wanted the date to display in text. I am the author of the script below. I wrote it to present the day and the month in text.
     As an added bonus, the script will add the "th", "st", "nd", or "rd" depending on the day of the month. I also used a new method to extract a four, rather than a two, digit year. It's a bit long, but it works pretty well.

Here are the goods:

And here's the script itself.

     Let's start from the top. This script relies on two arrays. The first array lists the days of the week and the second array lists the months of the year. Here's the first array, the days of the week:

DaysofWeek = new Array()

     An array is just what you see, it's a listing of things the JavaScript can refer to later. This specific array is made of of text strings. Note that each day of the week is surrounded by double quotes. Those double quotes tell the browser that what is contained within should be treated as just text. However, arrays can contain just about any text, command, number, or combination of the three you want.

     Each array starts with a line that announces to the browser that an array is starting. That line looks like this:

DaysofWeek = new Array()

     The format is similar to setting up a new Date(), which we'll actually do later. The line announces a new Array() and gives the array a name. In this case the name is DaysofWeek. I made the name up just like I would a variable name, because in reality it is a variable name. It's just that this variable has multiple values.

      You probably know this by now, but just in case...JavaScript counts everything and it starts counting at zero. Notice that the seven days of the week are numbered zero through 6. It's what's called an "index number". The format is to offer the name of the array, square brackets around the index number, and then the item that is being indexed. In this case it's a text string so the double quotes surround the text.

     But why is Sunday first? Why not Monday? The reason is that we are going to play into JavaScript's indexing system. If you've read over the JavaScript Primers or read over past Script Tips, then you're probably familiar with the getDay() method. If not, we'll get into it once again in this series.

     The getDay() method returns the day of the week only it returns that day as a numeric value, zero through 6 representing Sunday through Saturday. Now do you see the reason for the order of days? We've set up an array of days that is equal to the returns that getDay() will return. A little later we'll get into why this is important.

     Now that you're somewhat familiar with the format of an array, let's take a look at the second one. This one deals with the months of the year:

Months = new Array()

     The name of the array is Months and the months are arranged in order, zero through 11 because those are the numbers that are returned by using the getMonth() method.

     OK, now you're up to speed with the concept and structure of an array. We can begin calling for the array index through the getDay() and getMonth() methods.

Next Week: Pulling Out Part of the Array

     Do YOU have a Script Tip you'd like to share? How about suggesting a Script Tip to write about? I'd love to hear it. Write me at: [email protected].

Learn to write your own JavaScripts with the HTML Goodies 30-Step Primer Series