JavaScript by Example: JavaScript Core Objects. Pt. 1 | 6
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.
|
-
The getFullYear() method returns the year as 2003.
-
Another Date object called future is created. It will contain the future date, Christmas, passed as its argument.
-
The difference between the future time and the present time is calculated and returned in milliseconds with the getTime() method.
-
The Math object is used to round down the result of converting milliseconds to days.
-
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.
|
-
The variable now is assigned a number representing the day of the week, where 0 is Sunday.
-
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" .
-
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 .
-
A prototype method called DayOfWeek is assigned the name of the function that defines the method.
-
A new Date object is created with the Date() constructor method .
-
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