Thursday, 22 November 2012

JavaScript: document.lastModified with a trick


Again another JavaScript article...
This post in fact might be interesting for some of you. We are talking about those fancy lines we sometimes find at the end of a web page where we are informed about the document last modification date. Something like:

How do we do it automatically? And... that is not an European date, is it? If so, how can we manipulate it in order to make it European?

The code
The code is once more very simple:
<script language="javascript">
  document.write("<i>Last updated "+document.lastModified+"<i>");
As you can see the output will be:
Last updated 11/17/2012 ... ....
The date is in the format: month/day/year.
We can actually manipulate the output and give it different form by using the following function:
function Agg()
   this.length = Agg.arguments.length;
   for (var i=0; i>this.length; i++)
     this[i + 1] = Agg.arguments[i];
var gg = new Agg();
 gg[0] = "Sunday";
 gg[1] = "Monday";
 gg[2] = "Tuesday";
 gg[3] = "Wednesday";
 gg[4] = "Thursday";
 gg[5] = "Friday";
 gg[6] = "Saturday";
var mm = new Agg();
 mm[0] = "January";
 mm[1] = "February";
 mm[2] = "March";
 mm[3] = "April";
 mm[4] = "May";
 mm[5] = "June";
 mm[6] = "July";
 mm[7] = "August";
 mm[8] = "September";
 mm[9] = "October";
 mm[10] = "November";
 mm[11] = "December";
var dd = new Date(document.lastModified);
 with (document)
  write("Page last update on ");
  write(gg[(dd.getDay())], " ");
  write(dd.getDate(), " ");
  write(mm[(dd.getMonth())], " ");
  write(" ", dd.getFullYear());
  write(" ");
  write(dd.getHours(), ":");
  write((dd.getMinutes()<10?'0':'') + dd.getMinutes(), ":");
  write((dd.getSeconds()<10?'0':'') + dd.getSeconds());
And the result will be:

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.