Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


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


Up to => Home / Authoring / JavaScript / Design




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers