|
|
 |
There are many different date formats. Some consist only of numbers (e.g., 31/12/97), while others include real month names (e.g., December 31, 1997). There are many distinct date formats. In this section we'll show you how to display the date in a Month Day, Year format. This convention is usually better than a "number-only" one, due to the fact that different countries have different ways of writing the date in a "number-only" format. For example, the last day of the millenium could be written in one of the following ways: - 31/12/99 (in Israel, for example)
- 12/31/99 (in the US, for example)
What does 9/10/97 stand for? September 10, or October 9? It depends on where you live. The following script prints the current date in a Month Day, Year format.
<SCRIPT LANGUAGE="JavaScript">
<!--
function makeArray() {
var args = makeArray.arguments;
for (var i = 0; i < args.length; i++) {
this[i] = args[i];
}
this.length = args.length;
}
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}
function getString(date) {
var months = new makeArray("January", "February",
"March", "April", "May", "June", "July", "August",
"September", "October", "November", "December");
return months[date.getMonth()] + " " + date.getDate() +
", " + ((date.getYear() < 100) ? "19" : "") + date.getYear();
}
var cur = new Date();
fixDate(cur);
var str = getString(cur);
document.write(str);
// -->
</SCRIPT>
The makeArray() function is a user-defined constructor. It accepts a list of values (arguments) and puts them in an array. You could use the built-in Array() constructor instead, but it isn't supported by Navigator 2.0x.
The getString() function accepts a date, and returns a string in the Month Day, Year format. For example, it might return "May 5, 1980". The third global statment in the script assigns that string to a variable named str, and then prints it.
If you would like to display the date somewhere in an HTML table, you're in trouble. Never embed a script in a table, especially if it has a document.write() statement. At this time there is no workaround for this annoying bug (besides printing the entire table in JavaScript, or using Dynamic HTML).
      
|