Displaying the Render Date and/or Time in SharePoint Pages

This is a simple enough task with a little JavaScript.  Inserting the script below into your master page will output the date and time in the simplest way possible when the page is rendered:

   var dt = new Date();
  document.write(dt);

If your language is American English, you will see the following: Fri Oct 24 18:19:06 EDT 2008.  Of course, if you’d like things formatted a little more nicely, you can always expand the JavaScript.

Another nice trick is to use similar JavaScript to display the year information for the copyright notice that you are likely to have in your footer or elsewhere on your page.  The copyrightYear function below will return startYear if it is that year or earlier, and startYear-currentYear (e.g., 2007-2008) if the currentYear is greater than startYear.

// copyrightYear: Display a copyright year range.
// Arguments:
//		fieldName:	startYear
//
<!-- This Javascript function is used to display the copyright year range.
function copyrightYear(startYear) {
 // Get the current date
 var dt = new Date();
 var y  = dt.getYear();
 var yEndYear = "";
 // Y2K compliant
 if (y < 1000) y +=1900;
 // If the current year is greater the startYear, then display the range, otherwise just the current year
 if (y > startYear) yEndYear = "-" + y;
 document.write(startYear + yEndYear);
}

If you called the function like so today:

   copyrightYear("2007");

you would see 2007-2008.

You can see a working example of this on our demo page.

Similar Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

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