|
The Source
|
|
<SCRIPT
language="JavaScript"> |
|
|
Explanation:
First off, I needed to know some very important variables:
| Method Function | What the Function Does |
| getHours() | Returns the current number of hours into the day: (0-23) |
| getMinutes() | Returns the current number of minutes into the hour: (0-59) |
| getSeconds() | Returns the current number of seconds into the minute: (0-59) |
| getDay() | Returns the number of days into the week: (0-6) |
| getMonth() | Returns the number of months into the year: (0-11) |
| getYear() | Returns the number of years into the century: (0-99) |
This was crucial in order to write the JavaScript. These are not all of the method functions, but they are enough to create a decent clock. This JavaScript will only use the first three functions(Bummer!)
Once I have created an object, I am able to access its member functions with what is called "the dot operator". By placing the dot between the object name (thetime) and its member function, I am able to execute the member function. In this case, the function simply returns a value (the number of hours into the day). I am using this value as the value of our new variable, nhours.
I used the dot operator in the past, when I used the document.write() function. The write() function is a member function of the document object, and the function writes text to the screen.
An advantage of using objects is that objects can hold multiple values, where variables can only keep one value at a time. An object can also use any member functions it may have, like I did above. So, the date object named thetime can use all of the member functions. Now I can start getting the rest of the values for the clock.
Not bad, but now I needed to get into the extra coding so that the clock will run smoothly. The variable thetime.getHours() returns the number of hours into the day. This value is a number between 0 and 23. Sometimes, like this time, I want the clock not show in military time, but in regular US time, i.e 1:00PM not 13:00. That was the trick to this lil' part:
if (nhours>=13) nhours-=12;
nhours-=12; (is shorthand for: nhours=nhours-12;)
Also, if the hour is 0, it's 12 A.M., so I need to make the zero into a 12.
if (nhours==0) nhours=12;
Now, if, for example the current time is 14:00 military time, it subtracts 14-2 and gets the result 2. This is exactly what I wanted. The same problem isn't for the minutes and seconds, but they present a problem of their own. Supposing the number of minutes into the hour is 32. This value is fine, the script will display a 32. Yet, what if the value is 2? The current script would display something like this:
11:2
Yes, I needed a way to get a zero in there so the clock is readable. I want it to say 11:02. So now I need something to add in a zero before the 2 if the number of minutes (nmins ) is less than 10. This is what I came up with:
if (nmins<10)
nmins="0"+nmins;
This code will add in the character 0 in front of the 2 if the number of minutes is less than ten. Another little problem out of the way, but I needed to do this for the seconds:
if (nsecn<10)
nsecn="0"+nsecn;
if (nmins<10)
nmins="0"+nmins;
The clock displays A.M. or P.M. for morning and evening hours. This isn't to terrible. I can define another variable, so I called it AorP. I needed the value to be "P.M." if the hour is greater than 12, and "A.M." otherwise. Here goes:
if (nhours>=12)
AorP="P.M.";
else
AorP="A.M.";
As with anything there's always a catch. This section needs to be placed before the code that changes the clock to a twelve hour system. Otherwise, the hours will never get past 12, and it will always read A.M. Why? This's because when the clock changes to the twelve hour system, it changes the value of nhours so that it is 0-12 in all cases. So, it's necessary to get the AorP value before the clock changes the time system.
WHEW! That sure was alot wasn't it? Well, here it is again: