Automatically Update the copyright year on your website with some Simple Javascript.

javascript-time-copyrightMost of you hard core web developers would probably just do it with php/java/or C# but for those newbies that want something quick and dirty, I would recommend it with the client side so here’s a simple Javascript code that I put together. It was a small eye sore when I started noticing old school websites or even the simple ones where I just have a few pages that hard coded the date of the website year or copyright in the footer. You know what I’m talking about. The bottom says copyright 2011 © or the new update has been modified yet, the old year is still there.

So here’s my code that you stick in your global.js or Script folder. A simple function that will auto update the year or time of the date on your footer. You can place the ID pretty much anywhere on your site.


//start of script

function init() {
	// other functions...
	getCopyrightYear();
   }

   function getCopyrightYear() {
	var d = new Date();
	var thisYear = d.getFullYear();
	document.getElementById("copyright_year").innerHTML = thisYear;
        //you can also do this to keep it shorter. 
        //document.getElementById("copyright_year").innerHTML = d.getFullYear();
}

window.onload = init;
//end of script.

As far as the mark up in the HTML, it can be as simple as this.


        <footer>&copy; <span id="copyright_year"></span> Your Company</footer>

So I know some of you hardcore devs would probably use an “include” so you wouldn’t even worry about it at all but you might want to put this id for various places like the time or how often something happens during a week in a side bar.  It’s also good for single instances.  It’s pretty easy logic I think, It might use a little memory during the load time but the whole point of developing a website is so that we don’t hard code things that will change every year or every day. This has been tested for most major browsers.

One thought on “Automatically Update the copyright year on your website with some Simple Javascript.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.