WebDeveloper.com �: Where Web Developers and Designers Learn How to Build Web Sites, Program in Java and JavaScript, and More!   
Web Developer Resource Directory WebDev Jobs
Animated GIFs
CSS
CSS Properties
Database
Design
Flash
HTML
HTML 4.01 Tags
JavaScript
.NET
PHP
Reference
Security
Site Management
Video
XML/RSS
WD Forums
 Client-Side
  Development

    HTML
    XML
    CSS
    Graphics
    JavaScript
    ASP
    Multimedia
    Web Video
    Accessibility
    Dreamweaver
    General
    Accessibility
    Dreamweaver
    Expression Web

    General

 Server-Side
  Development

    PHP
    Perl
    .NET
    Forum, Blog, Wiki & CMS
    SQL
    Java
    Others

 Site Management
    Domain Names
    Search Engines
    Website Reviews

 Web Development
  Business Issues

    Business Matters

 Etc.
    The Coffee Lounge
    Computer Issues
    Feedback




DR. WEBSITE: Sharp Readers Point To Potential Problems With Last-Modified Code

By David Fiedler and Scott Clark

We were reminded recently that Dr. Website has sure got some sharp readers: Our column in the Sept. 29 issue ["Automatic Date and Time Displays," page 35], featured some JavaScript code for showing the last-modified date of a document. As programmers, we sometimes fall into a trap of thinking that we've covered all bases, when it takes a second pair of eyes to recognize a potential problem.

Of all the letters we received about the column, only one reader, Paul Morgan, noticed that if the last-modified hour was before noon, the script reported an undefined hour. He fixed this bug with the following code, which checks the hour to see if it is indeed before noon, and if it is, it adjusts from the original hour:

 var ampm; if(hour>12) { hor=hour-12; ampm="pm"; } else { hor=hour; ampm="am"; } 
As you can see, Paul also added an a.m./p.m. check to the code so that the exact time can be noted. He then added
 +ampm 
to the print statement at the end of the code.

Also, as many programmers from the old school are now finding out, the year 2000 problem has many far-reaching effects. How could it affect us as Web developers? Well, we heard from several readers who pointed out that we had created a year 2000 problem with our solution to the same question. One reader did more than point out our error--he sent us what he thought was the code to fix it. In our original column, we had used this code to actually write out the last-modified date in the form specified in the original question:

 document.write("<FONT SIZE=4 FACE=\"ARIAL\"><B>Updated " +mon + " " +day + ", " + "19"+year + " at " +hour + ":" +min + "</B></FONT>"); 
This works fine, but it would fail miserably if our readers used this code into the 21st century. Here's how one reader tried to make the code year-2000-compliant:
 if (revdate.getYear() <= 1999) { year=1900 + revdate.getYear() } else { year= revdate.getYear() }; 
Unfortunately, Netscape Navigator will only report the last two digits (or single digit if under 10) of the year using getYear( ), and in the year 2000 it would report "zero," which is considerably less than 1999, so the code would still fail to work properly. Microsoft Internet Explorer 3.0 returns the year minus 1900, and never represents years before 1970; IE 4.0 reacts the same as Navigator.

So how can we fix this potential problem? Well, one easy way would be to simply change the "19" in the original code to "200" for the first nine years of the 21st century, and "20" thereafter. We can hope that Netscape and Microsoft will have a more specific way of knowing the last-modified date, especially right around the turn of the century. Until then, keep your eyes open for these types of errors. They can bite you when you least expect it.

Webdeverloper.com's Home Page


RELATED STORIES:

Keywords: html
Date: 19971027



HTML5 Development Center


Recent Articles