spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java_core / 1 To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

Senior Systems Engineer – Disk-Based Backup/Replication (PA)
Next Step Systems
US-PA-Philadelphia

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


JavaScript by Example: JavaScript Core Objects. Pt. 1

Manipulating the Date and Time

JavaScript stores dates in milliseconds, so if you have more complicated calculations to perform, such as the number of days before a date, or between two dates, the information in Table 9.4 might be helpful in converting milliseconds to minutes, hours, days, and so forth.


Basic units of time.

Unit of Time

Milliseconds

 

1 second

1000

 

1 minute

second * 60

(1000 * 60)

1 hour

minute * 60

(1000 * 60 * 60)

1 day

hour * 24

(1000 * 60 * 60 * 24 )

1 week

day * 7

(1000 * 60 * 60 * 24 * 7 )

 

<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.)

The number of days between two dates has been calculated.

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.


home / programming / java_core / 1 To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

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

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