[JavaScript]
[Previous page] [Section contents page] [Next page]
Example 1: Last Modified Date Variations

We now have the conceptual tools necessary to start creating some simple scripts. The next few pages will walk you through some examples, and then point you to some resources for advanced JavaScript topics.

Last Modified Date Example

One of the most tedious tasks in writing for the web is remembering to replace the "Date Last Modified" note for pages for web pages when they are revised -- it's important, and it's not difficult, but remembering to do it is hard if you edit dozens or hundreds of pages a week. This is precisely the kind of mundane, repetitive task that calls for scripting.

The basic script here is very simple, since document.lastModified is a built-in object property. All we have to do is get the value and write it at an appropriate place in the document, along with any label we want to add:

    <script language="JavaScript">
<!--
document.write("Last modified " + document.lastModified);
// -->
</script>

This script returns the following (for the current page and your browser):

This may well be good enough for most cases, but notice that the date is not in the format many would prefer, and that you get the time -- down to the hundredth of a second -- whether you want it or not. With a little manipulation of the data, however, we can put it into a better form. Here is a script written by Bernhard Friedrich that presents the last modified date in nicer form:

<script language="JavaScript">
<!--
	// new options introduced by Bernhard Friedrich; should work in all browsers
	// additional code to display date in Month Day, Year format by Robert Crooks
	// Define varibles for the finished date string, the date string returned
	// by the browser, the month, day, and year
	var lutext;
	var lutime;
	var ludm;
	var ludd;
	var ludy;
	function sstr(a,b){ //extract substrings
		// function converts month string to appropriate number
		ret=lutime.substring(a,b);
		if (ret=="Jan" || ret=="01") ret="1";
		if (ret=="Feb" || ret=="02") ret="2";
		if (ret=="Mar" || ret=="03" || ret=="Mrz") ret="3";
		if (ret=="Apr" || ret=="04") ret="4";
		if (ret=="May" || ret=="05" || ret=="Mai") ret="5";
		if (ret=="Jun" || ret=="06") ret="6";
		if (ret=="Jul" || ret=="07") ret="7";
		if (ret=="Aug" || ret=="08") ret="8";
		if (ret=="Sep" || ret=="09") ret="9";
		if (ret=="Oct" || ret=="Okt") ret="10";
		if (ret=="Nov") ret="11";
		if (ret=="Dec" || ret=="Dez") ret="12";
		return ret;
	}
	lutime = unescape(document.lastModified);
	// the following structures determine the browser type and version
	// according to the length of the last.modified date string,
	// then extract the month, day, and year information from the string
	if (lutime.length == 17) { // Netscape 3 and higher, Internet Explorer 4
	ludm = sstr(0,2); 	
	ludd = sstr(3,5);	
	ludy = sstr(6,8);
	}
	if (lutime.length == 25 || lutime.length == 24) { // Netscape 2 or IE 3 (US)
	ludm = sstr(4,7);
	ludd = sstr(8,10);	
	ludy = sstr(20,24);
	}
	if (lutime.length == 29) { // Opera 3
	ludm = sstr(8,11); 	
	ludd = sstr(5,7);	
	ludy = sstr(12,16);
	}
	if (lutime.length == 24) { // Internet Explorer 3 (German)
	ludm = sstr(3,6);
	ludd = sstr(7,9);
	ludy = sstr(19,23);
	}
	lutext = "";
	
	// US date format
	// defines full English names for the months	
	monthName = new Array(12)
	monthName[1] = 'January'
	monthName[2] = 'February'
	monthName[3] = 'March'
	monthName[4] = 'April'
	monthName[5] = 'May'
	monthName[6] = 'June'
	monthName[7] = 'July'
	monthName[8] = 'August'
	monthName[9] = 'September'
	monthName[10] = 'October'
	monthName[11] = 'November'
	monthName[12] = 'December'
	yearNow = null
	// initialize yearNow;
	// then determine whether it's the 20th or 21st century,
	// if the year is in two-digit form
	// (note that if this script is still around in 2090,
	// it will have to be modified for the next century;-)
	if (ludy.length==2) {
	if (ludy >= 90) {
	yearNow = 19
	}
	else {
	yearNow = 20
	}
	}
	else yearNow=""	
	// construct the modified last modified date string
	lutext += monthName[ludm] + " " + ludd + ", " + yearNow + ludy + " ";		
	// write the string to the HTML document	
	document.write("Last Modified " + lutext);
	
	
// -->
</script>
	

The script returns the following string (for this page)

[Previous page] [Section contents page] [Next page]