01 September 2011

Date and Time in JavaScript

And this time I will present a snippet of code in javascript that shows the date and time.


You can put it wherever you like - in nearly every segment SharePoint Designer or in the Content Editor WebPart.

The JS code shows Date and Time

<!-- Clock --><script language="JavaScript">
var
clockID = 0;
function
UpdateClock() {
  
if(clockID) {
     
clearTimeout(clockID);
     
clockID  = 0;
  
}
  
var tDate = new Date();
  
var roiClock = window.document.getElementById("roiClock");
  
roiClock.innerHTML = ""
   
+ tDate.toLocaleDateString()
   
+ "&nbsp;&nbsp;&nbsp;&nbsp;"
    
+ tDate.getHoursTwoDigits() + ":"
    
+ tDate.getMinutesTwoDigits()
  
clockID = setTimeout("UpdateClock()", 6000);
}

function
StartClock()
{

  
clockID = setTimeout("UpdateClock()", 500);
}

function
KillClock()
{

  
if(clockID) {
     
clearTimeout(clockID);
     
clockID  = 0;
  
}
}
StartClock();

// return Minutes with two digits (chars)

Date.prototype.getMinutesTwoDigits = function()
{
    
var retval = this.getMinutes();
    
if (retval < 10)
    
{
 
         return ("0" + retval.toString());
     
}
     else     {
        
return retval.toString();    
    
}
}

// return Hours with two digits (chars)

Date.prototype.getHoursTwoDigits = function()
{
     
var retval = this.getHours();
     
if (retval < 10)
     
{
         return ("0" + retval.toString());
     
}
     
else    
     
{
        
return retval.toString();
     
}
}

</
script>
<
div id="roiClock" style="color:#888;font-size:13pt;font-weight:bold;width:300px;" />
<!--- End Clock -->

Some code taken from this - http://www.htmlfreecodes.com/
Roi

No comments:

Post a Comment