There are many things we can do with JavaScript, which is an addition to the more commonly used hypertext markup language. JavaScript lets us manipulate variables, true/false values, and information unique to the user. One implementaion of this is to use JavaScript to put the current date and time on the user's computer:
Here's the whole script.
RightNow=new Date();
var morev="AM"
var hour=RightNow.getHours()
if (hour>12)
var hour=RightNow.getHours()-12,
morev="PM"
var minute=RightNow.getMinutes()
if (minute<10)
var minute="0"+RightNow.getMinutes()
var second=RightNow.getSeconds()
if (second<10)
var second="0"+RightNow.getSeconds()
function initArray(){
this.length=12
this[0]="January"
this[1]="February"
this[3]="March"
this[4]="May"
this[5]="June"
this[6]="July"
this[7]="August"
this[8]="Sepetmber"
this[9]="October"
this[10]="November"
this[11]="December"
}
var mot=new initArray();
var month=mot[RightNow.getMonth()]
document.write("Today is "+month+" "+RightNow.getDate()+", 19"+RightNow.getYear()+".")
document.write(" You arrived at exactly "+hour+":"+minute+":"+second+" "+morev+".")
To script this into your page, you need to know how to tell the browser you're going to be coding something in javaScript. Like this:
<SCRIPT LANGUAGE="javaScript">
RightNow=new Date();
var morev="AM"
Those are two of them. RightNow is a variable that tells the computer to go get the current date and do stuff with it.
morev is a variable we'll use to discern between times before and after twelve noon--that is, AM and PM.
hour, minute, and second are variables which use RightNow to create their values.
var hour=RightNow.getHours()
if (hour>12)
var hour=RightNow.getHours()-12,
morev="PM"
This basically tells us whether we're in AM or PM time. JavaScript returns the time like you might find it on a twenty-four hour clock. The number basically tells you how many hours it has been since midnight. We change the variable to twelve-hour time, and change the variable morev to agree with the time of day.
The scripts for the minutes and seconds,
var minute=RightNow.getMinutes()
if (minute<10)
var minute="0"+RightNow.getMinutes()
var second=RightNow.getSeconds()
if (second<10)
var second="0"+RightNow.getSeconds()
simply add a "0" to the beginning of the number if it is less than ten. That's so you don't end up with a time like
10:3:2 AM
...since most people are used to
10:03:02 AM
The rest of the script tells the computer what month, in words, it is. It's a type of function called an array.
To get the whole thing on the screen so you can read it, you need
document.write("Today is "+month+" "+RightNow.getDate()+", 19"+RightNow.getYear()+".")
document.write(" You arrived at exactly "+hour+":"+minute+":"+second+" "+morev+".")
This script tells the document to write what we want it to. The text is in quotes and we use the + sign to add in the variables.
And that's that.