What Else Can You Do With The Date? - Page 7
December 7, 2001
In the night and day script, we worked with the time in hours. Of
course, JavaScript lets you access all parts of the date and
time, but the syntax isn't exactly plain English. Table 1-2 shows
how to get the various parts of the date and the form in which
they're returned.
Table 1-2: Getting the time from JavaScript
| Unit of time |
How to get it |
How to use it |
| Second |
second = now.getSeconds( ); |
The time in seconds is returned as a number 0 through 59. |
| Minute |
minute = now.getMinutes( ); |
The time in minutes is returned as a number 0 through 59. |
| Hour |
hour = now.getHours( ); |
The time in hours is returned as a number 0 (midnight) through 23 (11 P.M.). |
| Day |
day = now.getDay( ); |
The day of the week is returned as a number 0 (Sunday) through 6 (Saturday). |
| Month |
month = now.getMonth( ); |
The month of the year is returned as a number 0 (January) through 11 (December). |
| Year |
year = now.getFullYear( ); |
The year as a full four digit year (e.g., 1998, 2001). |
Keep in mind that when you get times and dates from JavaScript,
they are returned as numbers, not words. This means that if you
ask a Date object for the day of the week, using
getDay( ), you get a number 0 through 6, not the
name of a day, like Sunday or Monday. Though numbers are useful
for database applications and the like, you may want to put them
into a more digestible form. For example, you can create a script
that uses getDay( ) in combination with
if statements to translate the numeric values to
their actual names, as shown in Example 1-4.
Example 1-4: Connecting number values to day names
<script language="JavaScript">
var now = new Date( );
var day = now.getDay( );
var dayname;
if (day == 0) {
dayname = "Sunday";
}
if (day == 1) {
dayname = "Monday";
}
if (day == 2) {
dayname = "Tuesday";
}
if (day == 3) {
dayname = "Wednesday";
}
if (day == 4) {
dayname = "Thursday";
}
if (day == 5) {
dayname = "Friday";
}
if (day == 6) {
dayname = "Saturday";
}
document.write("Today is " + dayname + ". <br>");
</script>
So how does this work? First, a new Date object
named now is created, and the day of the week, which
is in number form, is given to a variable named day.
Then a series of if statements matches up the number
of the day with the day's full name and stores the name in the
variable dayname. For example, if day
is 0, it must be Sunday; if day is 1, it must be
Monday, etc. Finally, the day's full name, in variable
dayname, is displayed on the page using
document.write( ). Note that this script creates
the variable dayname without giving it a value at
the same time; the value is assigned later when we actually
figure out what day it is.
You can use this technique for various parts of the date to
create a script that displays the fully formatted date on the
page (e.g., Monday, July 30, 2001). The best way to do this
involves arrays, something you'll learn more about later in this
book. If you're curious, see "Doing the date right," in Chapter
5.
Document Properties - Page 6
Designing with JavaScript, 2nd Edition
Objects, Properties, And Methods - Page 8
|