JavaScript by Example: JavaScript Core Objects. Pt. 1 | 6

JavaScript by Example: JavaScript Core Objects. Pt. 1

 

<html><head><title>Countdown 'till Christmas</title></head>

<body bgColor="#00FF99">

<font face="arial" size=5 color=red>

<script language="JavaScript">

1 var today = new Date();

2 var fullyear = today.getFullYear();

3 var future = new Date("December 25, "+ fullyear);

4 var diff = future.getTime() - today.getTime();

// Number of milliseconds

5 var days = Math.floor(diff / (1000 * 60 * 60 * 24 ));

// Convert to days

6 var str="Only <u>" + days + "</u> shopping days left

\'til Christmas! ";

document.write(str+"<br>");

</script>

</body>

</html>

 

A new Date object called today is created.
  1. The getFullYear() method returns the year as 2003.

  2. Another Date object called future is created. It will contain the future date, Christmas, passed as its argument.

  3. The difference between the future time and the present time is calculated and returned in milliseconds with the getTime() method.

  4. The Math object is used to round down the result of converting milliseconds to days.

  5. This string contains the number of days between the present date and Christmas. (See Figure 9.15.)

Customizing the Date Object with the prototype Property

The Date object has a prototype property that allows you to extend the capabilities of the object. You can customize the time and the date by providing new methods and properties that will be inherited by all instances of this object. Since the Date object provides methods that return zero-based months, weeks, years, and other measures you may want to create a prototype method where "January" is month number 1 instead of 0, and the day is "Monday" instead of 1, etc.

 
 

 

<html><head><title>The Prototype Property</title>

<script language = "javascript">

// Customize the Date

1 function weekDay(){

2 var now = this.getDay();

3 var names = new Array(7);

names[0]="Sunday";

names[1]="Monday";

names[2]="Tuesday";

names[3]="Wednesday";

names[4]="Thursday";

names[5]="Friday";

names[6]="Saturday";

4 return(names[now]);

}

5 Date.prototype.DayOfWeek=weekDay;

</script>

</head>

<body bgcolor="pink">

<font face="arial" size="+1">

<center>

<script language="JavaScript">

6 var today=new Date();

7 document.write("Today is " + today.DayOfWeek() + ".<br>");

</script>

</body></html>

 

 
The function called weekday() is defined.
  1. The variable now is assigned a number representing the day of the week, where 0 is Sunday.

  2. A new Array object called names is created. It will contain seven elements. Each element will be assigned the name of the weekday, e.g., "Sunday"

    .
  3. The value in now, a number between 0 and 6, will be used as an index in the names array. If now is 6, then the value of names[6], "Saturday", will be returned

    .
  4. A prototype method called DayOfWeek is assigned the name of the function that defines the method.

  5. A new Date object is created with the Date() constructor method

    .
  6. The new prototyped method is called, and returns the string value of today's date, "Saturday". (See Figure 9.16.) The capabilities of the Date object have been extended to provide a method that will return the name of the weekday.

The day is converted to a string.


Created: March 27, 2003
Revised: November 19, 2003

URL: http://webreference.com/programming/java_core/1