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]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

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

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