JavaScript for Displaying Date Document Last Modified

It is easy to display the date and time that the current document was last modified in JavaScript. You simple need to embed the following into your HTML:

<script type="text/javascript">document.write (document.lastModified);</script>

Unfortunately, how this is displayed depends on the browser rendering the page. e.g.

Browser Display Format
Opera Sat, 14 Aug 2004 19:39:36 GMT
Mozilla Saturday, August 14, 2004 19:39:36
Internet Explorer 08/14/2004 19:39:36
Netscape Saturday 14 August 2004 19:39:36

This JavaScript routine displays the date in a consistent format, either with or without the time fields as follows:

Calling Sequence Result
<script type="text/javascript">writeDateModified (false);</script>
<script type="text/javascript">writeDateModified (true);</script>

It is worth noting that the document.lastModified property is populated from the Last-Modified HTTP header supplied by the web server. If that header is provided, then the routine works as expected in all browsers. Without that header, JavaScript has no way of knowing the last modified date, and most browsers will instead return the date the page was loaded that is to say, they provide an incorrect result. Safari and Chrome do a more sensible thing: if the last modified date isn't known, then the lastModified property is left blank. This routine reflects this, and says that the information isn't available.

The zip file includes the English version, and a Dutch version kindly made available by Jeanette Gerritsen.

Download compressed JavaScript file (2.44KB)