|
|
He u ever wanted the power of determining the current time
or date to manipulate how it's displayed on your site?? Yes?? Well, today's ur lucky day. Objects of the Part 1 First, let's open up a text editor, one of our favorite and most useful tools. We shall start by beginning the HTML page's header. <HTML>
Now, let's create the body with da' white background 'n black text. We'll also add text to display above da' time. <BODY BGCOLOR="#FFFFFF"
TEXT="#000000">
Now for da' JavaScript. Remember, what I said 'bout creating a new Date object
using the <SCRIPT
LANGUAGE="JavaScript">
Now that we have our Date object all ready ( hurray.. ^_^), let's make use of some of the
many methods that come with it. Up to this point, you've probably been wondering
why we're using the Date object when we're trying to obtain the current time,
not the date. Let's list some of the methods we'll be using: To access an object's methods, we must use the dot operator(.) followed by
the name of the method and any parameters necessary or desired. In this case,
the methods we'll be using are, as listed above, //Use the getHours() method to get
the current hour.
Great. We've got our variables set. We're ready to display the time, right? Oh, you wish. JavaScript works on a 24-hour clock. Now if you'd prefer a 24-hour clock, all's well and dandy, but for those who don't, we'll have to make some adjustments as well as determine AM or PM. Oh, and one other thing. For time digits less than 10, JavaScript does not add a zero to the tens place. That's something we shall fix, as well. //If the hour is greater than or
equal to 12, set
Well, fellow scripter, r u with me so far? Hm... I guess now that we've set up our script to determine the time based on a 12-hour clock, u think we're ready to display it, eh? You're right. We are. //Write the current time to the
screen.
That's it! Just end your script and HTML document, and you're done... for now. //-->
|
Send me an e- mail to naruse_chan@hotmail.com or use
feedback form (feel
free to contact me) with
questions or comments about my web site.
|