|
(Lesson 8) I think that you will find this lesson and the next to be a lot of fun. This lesson introduces you to two stand alone objects, the Date Object and the Array Object. You will learn how to extract a date and time from the Date object. The next lesson will continue our discussion of the Date object and will show you how to make several different clocks.
The Date object is a very powerful stand alone object that you will use often. You will benefit if you take some time and understand just how to
Use the following syntax to create a Date object The Date object you create is a snapshot of an exact millisecond in time. It contains information on both date and time. It is important to understand that once the Date object is created, the date and time that it contains does not change unless acted on by one of its many methods. If the parameters are left out when you create a Date object as shown
below, the object then contains the date and time that your computer clock
is at. You can also create a Date object for a specific date and time. You
will want to do this to be able determine specific information on a
particular date such as what day of the week it falls on and to allow you
to do date math such as calculating the number of days between two dates.
The 5 ways to create a Date object for a specific date and time are shown
below: Here is a example of creating a Date object for each of the 5 ways:
In each of the first 4 examples the date May 1, 1999 gets stored in the object when it is created. A time, 12 noon, is also stored in the first and third examples. Notice that I used the number 04 in the third and fourth example for the month. In JavaScript, counting for the months starts with 0. Therefore the first month, January, is 0 and May is 4. The last example probably looks a little strange to you. It is important to realize that all time in JavaScript is referenced to midnight, January 1, 1970, Greenwich Mean Time. As stated before, this elapsed time is measured in milliseconds. Therefore this example sets the time to 100 milliseconds past midnight GMT, January 1, 1970.
Try creating a Date object in the lab using each of the methods shown above. Remember you can use the document.write() method to display your results. Try the last one with the number 0. This should yield the date and time that the count for the Date object was started. You might be surprised that the answer is not the expected January 1, 1970 at 12 am. Why is yours different? Give up? Here's the answer. The Date object does not have any properties but it has 21 methods, listed in the following table. I have grouped them according to how they are used. Use the first 8 methods to extract specific date and time information from the object. The next 8 methods allow you to change specific information in the Date object. The last 5 methods are rather specialized.
I recommend that you go back to the lab and try some of the above
methods so that you will understand them better. I will start you out.
Type the following in the proper place in between the script tags in the
BODY section. You will need a little different setup to test the second set of
methods. This is one that should work. Take another look at the "Date Object Methods" table above. What if I
wanted to store all this data in a variable so that I can easily
recall it. How would I do it? The answer is to use an Array. The Array is
ideal for storing the type of data that is contained in a table. In
programming, an array is defined as an ordered collection of data.
Suppose I wanted to store the days of a week in an Array. It could not
be easier. All I have to do is make a new Array and populate its elements
at the same time. Here's how to do it. Getting the data out of the array is just as easy. You refer to a
particular element of data like this For instance daysOfWeek[3] would yield Wednesday.
You can declare an Array object and add the elements later if you want
like this. The value of the elements can be of any type. The values of the
different elements of the same Array can be of different types too.
The Array object has two properties, length and prototype. The length
property tells you how many elements the Array contains. For instance in
our example the value of daysofWeek.length would be 7 assuming that we finished the
Array by putting all of the days in it. The prototype property is a little
complex and will not be covered here.
The array object has 9 methods: concat, join, pop, push, shift,
unshift, reverse, slice, and sort. The sort method does what it's name
implies and sorts the array. The reverse is a reverse sort. These methods
are more complex than I have implied here but will work as advertised on
the simple arrays we are dealing with here. The remaining 7 methods of the
Array object are beyond the scope of this class.
Note: See Appendix 4 to
learn how to make your date scripts Y2K compatible.
|