<TAG>Simple HTML& Javascripting Tricks & Tips

 

JavaScript: Adding a Date Stamp

Jack Donnell,
[email protected]

Looks like every web page has a Date Stamp on it. A pretty handy trick for most any page.  It almost makes the viewer think that the page was recently updated.  Play around sometime and load a page with a date stamp, then change your system clock and reload it. Trash In. Trash Out. 

The code is pretty simple to follow. You can add color and change the font and positioning with <p align="center"></p>(<center></center> will work, too)  and <font face="Arial" size="3' color="></font> tags at both ends of your <script></script> tags.

Okay, my coding is not pretty. I did not add any browser checks and what not. I'm bad..very bad. I just wanted to show you how the write this bit of code into your pages.  I use this bit of code a lot.  Mostly, as a call from an external *.js file.  I'm pretty lazy when I code and this keeps me from having to cut and paste or retype a script. There are some downfalls and I go better into some of them in External JavaScripting.

The sample code bellow is in it's elementary form and will write the date stamp to your page. You can just cut and paste it into a text file with the htm or html extensions and it should work, but remember that you can put this most anywhere on your page and format it anyway you like it. So, play around with it.  I did put some extra notes in the code behind the //'s. Biggest thing to rememeber is the Day, Month and any Arrays for that matter must be typed on only one line. Line Breaks in an array prevent the entire script from working.

BLAH..BLAH.. I know..Hush Up and Give you the code. Here it is.

Return to JackDonnell.com


SOURCE CODE

<html>

<head>

<title>Creates TIMEDATE STAMP for WEB PAGES</title>

</head>

<body bgcolor="ffffff">

<script LANGUAGE="Javascript">

<!-- Hiding from those old Browsers
// Y2K READY-USE GetFullYear
// DAY Names Javascript is funny Starts the numbering with Zero this array translates to 0...6 to the days of the week

// REMEMBER Arrays have to be written all on ONE(1) line to work

var stampdays = new Array( "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

// Month Names - Guess what this array does. 0..11 to the system clock month

var stampmonths = new Array( "January","February","March","April","May","June","July","August","September","October","November","December");

// GRABS the Date info from your System clock when your Browser reads  enters the page.
var thedate = new Date();

//Gets the Translated Arrays written to the webpage for viewing. Remember you can use this for other things, too
document.write(stampdays[ thedate.getDay()] + ", " + stampmonths[ thedate.getMonth()] + " " + thedate.getDate() + ", " +   thedate.getFullYear());

// -->

</script>

</body>

</html>