writeModifiedDate — Automatically write the modified date

A large portion of the web pages on the Internet have a last modified or updated date near the bottom of the page. This is a convienent way to let visitors know how recent the page has been changed. Here is a JavaScript that automatically displays the last modified date. The date format used is the ISO 8601 international standard date format yyyy-mm-dd. (The script can easily be rewritten for any date format.) Now you don’t have to remember to manually change the date yourself. (See script discussion below.)

Download this script.

To install this script, follow these steps:

1. Cut and paste this script into the HEAD of your document:

<script type="text/javascript" language="javascript">
  <!--
 
  // Written by: Mark Woodward (October, 2002)
  // Woody's JavaScripts at: woody.cowpi.com/javascripts/
 
  function writeModifiedDate() {
    modDate = new Date(document.lastModified);
    modYear = modDate.getFullYear();
    modMonth = modDate.getMonth() + 1;
    modDay = modDate.getDate();
    if (modMonth < 10) modMonth = '0' + modMonth;
    if (modDay < 10) modDay = '0' + modDay;
    document.writeln('Updated: ' + modYear + '-' + modMonth + '-' + modDay);
    }
 
  //-->
  </script>

2. Cut and paste this script into the BODY of your document exactly where you want the modified date to appear:

<script type="text/javascript" language="javascript"><!--
  writeModifiedDate();
  //--></script>

Script Discussion:

What makes this function work is in the first line:

modDate = new Date(document.lastModified);

A web page document has many properties, but the lastModified holds the information needed, namely when was the file was last modified. If we convert it into a JavaScript Date object, then we can use the Date methods to find the year, month, and day. The rest of the function formats the date for display.