Share this Page URL

Chapter 17. The Programmable Page: Addin... > Some JavaScript Examples - Pg. 198

The Programmable Page: Adding JavaScripts to Your Pages 198 Figure 17.4. The JavaScript code creates a standard mailto link. Putting the Date and Time into a Page A similar example involves putting the current date and time into the page. The current date and time is produced by JavaScript's Date() statement. Here's a simple example that uses Date() (see jsdate1.htm on the CD in this book). Figure 17.5 shows Internet Explorer's interpretation. <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> <!-- document.write("You're visiting my Web page on " + Date()) //--> </SCRIPT> Figure 17.5. This simple script inserts the current date and time. That's not bad, but the date and time display is a bit ugly. To get more control, you need to break out the various components of the date and time: the day of the week, the month, the hours and minutes, and so on. Here's a quick summary of the JavaScript statements that do this: · getDay()--Returns a number between 0 and 6, where 0 is Sunday, 1 is Monday, and so on. · getMonth()--Returns the month number, where 0 is January, 1 is February, and so on. · getYear()--Returns the year component of a date. If the year is before 2000, it returns a two- digit year (such as 99); if it's in 2000 or later, Internet Explorer returns the four-digit year, but Netscape 4 and later subtract 1900 from the year (for example, 2001 returns 101). · getSeconds()--Returns the seconds component of a time. · getMinutes()--Returns the minutes component of a time. · getHours()--Returns the hours component of a time. Note that this is military time, so if it's 5:00 in the afternoon, it returns 17. To use these statements, you must first store the current date in what programmers call a varia- ble, like so: d = new Date()