|
The
Javascript Y2K problem :
You may have noticed that some dates
generated with Javascript have a Y2K problem; especially
when viewed with The Netscape browser.
This is a workaround that should function
with both Browsers
<script
language="JavaScript">
//This line tells the browser it is beginning a JavaScript
code section.
//
Begin
//This line is pretty much standard in all JavaScripts - it
tell 'ancient' browsers that don't support JavaScript to
ignore //the following script.
when =
new Date();
//This line set the day variable to a new Date. Since Date()
has no parameters -- nothing inside the parenthesis -- the
//default date is set to the exact moment the script is
loaded.
day =
when.getDate();
//This statement extracts the current day of the month, for
example the 3 in January 3, 2000
year =
when.getYear();
//And, this line will extract the current year correctly as
19xx for years less than 2000, but reads 100 for 2000.
//Netscape's getFullYear() function attempts to work-around
this, but here's a easier and more reliable solution until
//the getFullYear() function is supported by all major
browsers....
if
(year < 2000)
year = year + 1900;
//If the year is read correctly as 2000 (or more) in the
browser, everything is ok and the next line is ignored. If,
//however, the browser reads 100 instead, the if statement
evaluates to true (100 < 2000 "100 is less than
2000") //and the next line ads 1900 to it, to make it
2000. year equals year plus 1900, (100 + 1900 = 2000) then
you can //have any
document.write(year);
//statements you want, etc here. And, the year will be Y2K
compatible! Then, you finish the script tags, like this....
//
End -->
</script>
|
|