Code Snippet: Change Dates Automatically

Recently, I was updating some of my small on-page javascripts to make them unobtrusive.  The three given here are completely unnecessary, but using them helps take some of the burden off a webmaster, so I decided to put them up for you as code snippets. The task that they perform could easily be done with SSI, but then your webpage would need to be capable of using it, and the include code (plus the formatting of the date) would have to be on every page.

If you have a copyright year or a current or last-modified date posted on your web pages, these scripts will automatically update them for you.  This means you won't have to remember to change them at year's end, or each time you update a page. 

 1 function currentYear()
 2 {  
 3   if (!document.getElementById("currentyear")) return false;
 4   var obj = document.getElementById("currentyear");
 5   var today = new Date();
 6   var year = today.getFullYear();
 7   txt = document.createTextNode(year);
 8   obj.appendChild(txt);
 9   return true;
10 }

 1 function currentDate()
 2 {
 3   if (!document.getElementById("currentdate")) return false;
 4   var obj = document.getElementById("currentdate");
 5   var monthtext = new Array("January","February","March","April","May","June",
       "July","August","September","October","November","December");
 6   var today = new Date();
 7   var month = today.getMonth();
 8   var date = today.getDate();
 9   var year = today.getFullYear();
10   var currentdate = monthtext[month] + " " + date + ", " + year;
11   var txt = document.createTextNode(currentdate);
12   obj.appendChild(txt);
13   return true;
14 }

 1 function lastUpdated()
 2 {
 3   if (!document.getElementById("lastupdate")) return false;
 4   var obj = document.getElementById("lastupdate");
 5   var monthtext = new Array("January","February","March","April","May","June",
       "July","August","September","October","November","December");
 6   var modified = new Date(document.lastModified);
 7   var month = modified.getMonth();
 8   var day = modified.getDate();
 9   var year = modified.getFullYear();
10   var lastupdate = "Last update: " + monthtext[month] + " " + day + ", " + year;
11   var txt = document.createTextNode(lastupdate);
12   obj.appendChild(txt);
13   return true;
14 }

How to use the script:  

1) You don't need to make any changes to the above functions.  They are written as independent scripts, so any or all of them may be used.

2) Follow the instructions on the main Snippets Page for how to install a javascript function that runs at load time.

3) Put the following code on your webpage wherever you want one of the dates to appear.  I use a <span> tag, since then the date can be inserted on the same line as other text, but you can use a <p> or a <div> if this doesn't matter.

<span id="currentyear"></span>

See it in action:   You can see two of these scripts in use on this very page.  'lastUpdated' is at the top, and 'currentYear' is in the footer.  Take a look at the source code for this page if you want to see the <span> tags.