Javascript Data

Although the data which the javascript acquires is fixed depending on the area your viewer is in what you do with the data it totally under your control.

A script will only bring you numbers that you can manipulate. There is no requirement for you to use the full day or month names nor is there any particular order you need to display this information.

You could manipulate this if you were writing a "menu of the day" on your site e.g.:

if (day==4)
{ document.write("<h3>Thursday's Menu</h3><ul><li>Soup or Salad</li><li>Beef or Chicken</li><li>Choice of potato</li><li>mixed vegetables</li><li>Ice-cream or Pie</li><li>Coffee or Tea</li></ul>"); }

Notice there are no carriage returns within the menu coding.

Valid XHTML 1.0 Transitional

Date Javascript

The following is a simple Javascript program to fetch a date that you can include on your Web page. This is a particularly useful reference for sites with dates included in their content.

This first section is where the script collects the local area date data for your users. It is the same for all date scripts.

/* date script by*/
now = new Date();
localtime = now.toString();
day = now.getDay();
month = now.getMonth();
date = now.getDate();
year = now.getFullYear();

In this section the script turns the numbers it received from the previous section and turns them into words. NOTE: Sunday is the number "0" according to the computer rather than 1 or 7 and January is also a "0".

After this section has been processed the script will have produced, via the "document.write" statement, the day and month.

if (day==1) { document.write("Monday, "); }
else if (day==2)
{ document.write("Tuesday, "); }
else if (day==3)
{ document.write("Wednesday, "); }
else if (day==4)
{ document.write("Thursday, "); }
else if (day==5)
{ document.write("Friday, "); }
else if (day==6)
{ document.write("Saturday, "); }
else if (day==0)
{ document.write("Sunday, "); }
if (month==0)
{ document.write("January "); }
else if (month==1)
{ document.write("February "); }
else if (month==2)
{ document.write("March "); }
else if (month==3)
{ document.write("April "); }
else if (month==4)
{ document.write("May "); }
else if (month==5)
{ document.write("June "); }
else if (month==6)
{ document.write("July "); }
else if (month==7)
{ document.write("August "); }
else if (month==8)
{ document.write("September "); }
else if (month==9)
{ document.write("October "); }
else if (month==10)
{ document.write("November "); }
else if (month==11)
{ document.write("December "); }

In this section four variables have been set to represent the suffix's of any date. This is not necessary but adds a human touch to a date script.

var a="st,"
var b="nd,"
var c="rd,"
var d="th,"

Here the script takes the date digits and sees which variable to apply to it using an if:then:else statement. The second to last line contains the code else if (date!=1||...) the != bit means "does not equal and || means or.

if (date==1) { document.write("1" + a + " "); }
else if (date==2)
{ document.write("2" + b + " "); }
else if (date==3)
{ document.write("3" + c + " "); }
else if (date==21)
{ document.write("21" + a + " "); }
else if (date==22)
{ document.write("21" + b + " "); }
else if (date==23)
{ document.write("23" + c + " "); }
else if (date==21)
{ document.write("31" + a + " "); }
else if (date!=1||2||3||21||22||23||31)
{ document.write(date + d + " "); }

The script now writes the year

{ document.write (year); }

That's the basic script. Studying it carefully will make the pattern more clear. To use the script transfer the pieces of code to a blank file and save it as whatever.js the only requirement is that it must have the .js extension.

To call the output of this script enter this code into an element such as a <p> tag which can be styled however you want to control the appearance of the date.

<p><script type="text/javascript" src="date.js"></script></p>

A modification you could easily make to this script would be to have it recognise special dates and write an appropriate message or special greeting on certain days e.g.

else if (date==21)
{ document.write("31" + a + " "); }
else if (date!=1||2||3||21||22||23||31)
{ document.write(date + d + " "); }
{ document.write (year); }
if (date==31 && month==9)
{ document.write(" Happy Hallowe'en!"); }

You can add multiple special dates in this way. There is no need for else statements as there is no requirement for a day to be special whereas it is necessary for every day to have a date.