Friday, March 29, 2024

HTML Goodies: Script Tip: Week 31

 

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()
DaysofWeek[0]=”Sunday”
DaysofWeek[1]=”Monday”
DaysofWeek[2]=”Tuesday”
DaysofWeek[3]=”Wednesday”
DaysofWeek[4]=”Thursday”
DaysofWeek[5]=”Friday”
DaysofWeek[6]=”Saturday”

     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 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()
Months[0]=”January”
Months[1]=”February”
Months[2]=”March”
Months[3]=”April”
Months[4]=”May”
Months[5]=”June”
Months[6]=”July”
Months[7]=”August”
Months[8]=”September”
Months[9]=”October”
Months[10]=”November”
Months[11]=”December”

     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

    


Learn to write your own JavaScripts with the

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured