![]() |
I hear the song of the nightingale. The sun is warm, the wind is mild, willows are green along the shore, Here no bull can hide! What artist can draw that massive head, those majestic horns? |
While nowhere near the majesty of the oxherder's bull, the date and time display above reveal the presence of JavaScript. This page is definitely 'alive'. Before we look at the code that makes it work, let's visit the world of Objects, Statements, and Operators - the ones we'll need for a date and time display. [Remember that the key below shows you the code for this page].
The monk asked Joshu, 'Is there Buddah-Nature in a dog?' Joshu replied, 'Mu!' |
Method | Returns | format (numbers) |
---|---|---|
getMonth ( ) | month | jan=0 thru dec=11 |
getDate( ) | day | day of month |
getYear ( ) | year | year - 1900 |
getHours ( ) | hours | 0 to 23 |
getMinutes ( ) | minutes | 0 to 59 |
getSeconds ( ) | seconds | 0 to 59 |
To create one holding the time of its creation, use:
To create one holding some other moment, use:
if ... else |
---|
if (this is true){ ...do this... } else { ...do this... } |
The optional else clause is followed by a block to be executed. You may string together if ... else if ... else if ... expressions to emulate the 'Case' statements in other languages. There is a 'shortcut' for simple conditional statements:
Math Operators | Assignment Operators | Relational Operators | |||
---|---|---|---|---|---|
+ | addition | x+=y | x=x+y | == | equal |
- | subtraction | x-=y | x=x-y | > | greater than |
* | multiplication | x*=y | x=x*y | >= | greater than or equal to |
/ | division | x/=y | x=x/y | < | less than |
% | remainder | x%=y | x=x%y | <= | less than or equal to |
++ | increment | . | . | <> | not equal |
-- | decrement | . | . | ! | not |
. | . | . | . | && | and |
. | . | . | . | || | or |
Commentary: The code will now be quite 'readable'. The onLoad event in the body tag calls Update(). The Update() function initializes the Date Object to this instant. Using the Date Object's methods, we extract the necessary information; format it; print it to the page; and then set a timer to repeat the process in one second.
Notice the placement of the curly braces for clarity. [You don't have to use this much space]. The semicolons are optional if each command is on a separate line.
The only piece that need explanation is the addressing:
![]() |
![]() |
![]() |
![]() |
![]() |