Start | Time | IF | Date Object | Arrays | Avoid the work | Hide email | Random | Intigrate HTML | Test
The Date Object

JavaScript has several handy functions built right into it.
Here we deal with time.
To get the time JavaScript will read it from your own computer and then give it to you when you call for new Date ( )
This is where we must define a variable.
A variable is simply a name that JavaScript can use to store its data.
In this case we will use the name 'now' and have JavaScript fill it with all the info from your computer's clock.
now = new Date( )
Next we need to extract the part we need.
For now let us just find the year
We use another variable and fill it with the year like so
yr = now.getFullYear( )
'yr' is our new variable and it is reading getFullYear from the variable 'now'
To print this variable on the page we use document.write like this
document.write
(
yr
)
Notice that the yr is not in parentheses because it is a variable.
Now we have a variable 'yr' that we can play with but first let us put a variable with some regular text into document.write( )
All we do is put a + sign between variables and regular text.
'The year is now '+yr
To review this is how you would print the current year.
<Script Language="JavaScript">
<!--
now = new Date( )
yr = now.getFullYear( )
document.write
(
'The year is now ' + yr
)
//-->
</Script>

Ok, we can print out meaningful text with JavaScript so now let us play with a variable.
Let us figure out our age and print it for all to see.
Make a new variable 'age' and simply subtract your birth year from the current year 'yr'.
(I'll use my own )
age = yr - 1937
To print it on a page we simply print
document.write(
'I turn ' + age + ' this year'
)
To recap again this is how you would print your age
<Script Language="JavaScript">
<!--
now = new Date( )
yr = now.getFullYear()
age = yr - 1937
document.write
(
'I turn ' + age + ' this year'
)
//-->
</Script>



Next : Those little adjustments


Home