Start | Time | IF | Date Object | Arrays | Avoid the work | Hide email | Random | Intigrate HTML | Test
Avoiding doing it all again.

What I love about computer programming is the ease with which you can do complicated tasks again and again while only having to work it out once.
Javascipt is no different in this regard.
If you have managed to plough your way through these previous pages you will have realized that, while not really complicated, typing out the whole thing every time you want to do something with date or time would be very time consuming - not to mention the possibility of typos rendering your script useless.

To get over this we can make up a JavaScript page with all the pertinent information on it and simply call on it when we need something.
To make the page we will copy all the script from the previous page except for the opening and closing tags and the document.write() parts.
// read computer clock
now=new Date();
// assign the data to variables
hr = now.getHours( );
noon='am';
if (hr >11)noon='pm'
if (hr>12) hr=hr-12;
if (hr==0) hr=12;
mn = now.getMinutes( );
dy = now.getDay( ) ;
d = now.getDate( ) ;
mth = now.getMonth( )+ 1 ;
yr = now.getFullYear( ) ;
// Assign data to variables for Universal time
uhr = now.getUTCHours( );
unoon='am';
if (uhr >11)unoon='pm'
if (uhr>12) uhr=uhr-12;
if (uhr == 0 ) uhr=12;
umn = now.getUTCMinutes( );
udy = now.getUTCDay( ) ;
ud = now.getUTCDate( ) ;
umth = now.getUTCMonth( )+ 1 ;
uyr = now.getUTCFullYear( ) ;
// set up arrays
weekday=new Array ('Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' )
monthname=new Array('blank','January','February','March','April','May','June','July','August','September','October','November','December')
th_num = d % 10;
if (th_num>3 || d == 13) th_num = 0;
uth_num = ud % 10;
if (uth_num>3 || ud == 13) uth_num = 0;
th_name = new Array ('th' , 'st' , 'nd' , 'rd' );

Put this on a plain page and save as 'time.js'
Now all we need to do is call the JavaScript file like this.
<Script Language="JavaScript" src="time.js"></Script>
Then we can use any of the variables we know are in there for any reason on a page.
<Script Language="JavaScript">
<!--
document.write(
'Let me see! I think it must be the ' + d + th_name[th_num] + ' today'
)
//-->
</Script>



Now let us go back to what I wrote about my dog Buster on the first page of this tutorial.
If you remember I wanted to give his age, his age converted to dog years and how long ago he had his accident and not have to update the page every January.
It was as simple as this
<Script Language="JavaScript" src="time.js"></Script>
<Script Language="JavaScript">
<!--

// Buster's age born in 1991
oldage = yr-1991;
// His age converted to dog years ( 7 times actual )
age=oldage*7;
// How long ago he had his accident which was in 1998
ago = yr-1998;

document.write ( 'At ' + oldage + ' years old this Christmas ( that is ' + age + ' in our years) we finally have the jumping under control.<br> He would do it if we let him but now he listens to us. You see, ' + ago + ' years ago he slipped a disk while greeting us at the door. ' )
//-->
</Script>



Next - Hiding your email address

Home