Script Tip 31by 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. Here are the goods:
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() 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() 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
|