Website Designers R Us - Home
How to display the last modified date

So you want to display the date your page was last modified? It's just a matter of displaying the document.lastModified property in JavaScript, but displaying it in a more user-friendly format is going to take some code.

If you're in a hurry, simply copy and paste the following code on to your web page (where you want the last modified date to appear):

<script 
  type="text/JavaScript" 
  language="JavaScript">
<!-- 
//
// format date as dd-mmm-yy
// example: 12-Jan-99
//
function date_ddmmmyy(date)
{
  var d = date.getDate();
  var m = date.getMonth() + 1;
  var y = date.getYear();

  // handle different year values 
  // returned by IE and NS in 
  // the year 2000.
  if(y >= 2000)
  {
    y -= 2000;
  }
  if(y >= 100)
  {
    y -= 100;
  }

  // could use splitString() here 
  // but the following method is 
  // more compatible
  var mmm = 
    ( 1==m)?'Jan':( 2==m)?'Feb':(3==m)?'Mar':
    ( 4==m)?'Apr':( 5==m)?'May':(6==m)?'Jun':
    ( 7==m)?'Jul':( 8==m)?'Aug':(9==m)?'Sep':
    (10==m)?'Oct':(11==m)?'Nov':'Dec';

  return "" +
    (d<10?"0"+d:d) + "-" +
    mmm + "-" +
    (y<10?"0"+y:y);
}


//
// get last modified date of the 
// current document.
//
function date_lastmodified()
{
  var lmd = document.lastModified;
  var s   = "Unknown";
  var d1;

  // check if we have a valid date
  // before proceeding
  if(0 != (d1=Date.parse(lmd)))
  {
    s = "" + date_ddmmmyy(new Date(d1));
  }

  return s;
}

//
// finally display the last modified date
// as DD-MMM-YY
//
document.write( 
  "This page was updated on " + 
  date_lastmodified() );

// -->
</script>
 
Result:
 
This page was updated on 11-Oct-06
 
 

     How does it work?
 
  • First we check the document.lastModified property for a valid date using "Date.parse()". This check is necessary because some web servers may not return the last modified date. Also, some browsers may suppress this information for security reasons.
     
  • Then we simply call "date_ddmmmyy()" function to format document.lastModified into a more readable date.

 

     NOTE: To get around Year 2000 (Y2K) issues related to the date returned by the document.lastModified property and JavaScript implementations of some browsers, we're displaying the default 2 digit year in this example. Even the "getFullYear()" function used to get the 4 digit year does not work as documented on all browsers when combined with document.lastModified.

Return to Listing

Website Designer R Us has over 12 years of IT experience and a focus on custom website design, web development and web hosting services. Our professional web design services will give your business the look and feel needed to beat your competitors! Our website design services include; web design, website redesign, website maintenance, web development, flash animation, eCommerce, shopping carts, domains, web hosting, search engine optimization, graphic design, logo design, blog writing, script installations & much more!
 Home          ::             About Us          ::             Support            ::             Services            ::             Link Partners          ::             Contact

Copyright © 2006-2007 Website Designers R Us, a DOT Specialist Company. All rights reserved.