Tuesday, February 8, 2011

Time Zone Webpart (CEWP)

In the CEWP add this code and in any document library add timezone.js and reference it properly

<style type="text/css">
 th {filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfff4', endColorstr='#b3bead',undefined ); /* ie */ }
</style>
<table align="center" border="1" bordercolor="black"cellpadding="0" cellspacing="0" width="100%">
<tr>
 <th align="center" valign="middle">EST</th>
 <th align="center" valign="middle">CST</th>
 <th align="center" valign="middle">MST</th>
 <th align="center" valign="middle">PST</th>
</tr>
<tr>
 <td align="center" valign="middle"><span id="tEST">&nbsp;</span></td>
 <td align="center" valign="middle"><span id="tCST">&nbsp;</span></td>
 <td align="center" valign="middle"><span id="tMST">&nbsp;</span></td>
 <td align="center" valign="middle"><span id="tPST">&nbsp;</span></td>
</tr>
</table>

<script type="text/javascript" src="js\TimeZone.js"></script>





TimeZone.js:



function timeSource(tz){
   x=new Date(timeNow().getUTCFullYear(),timeNow().getUTCMonth(),timeNow().getUTCDate(),timeNow().getUTCHours(),timeNow().getUTCMinutes(),timeNow().getUTCSeconds());
   switch(tz)
   {
  case 0: /*PST*/
   x.setTime(x.getTime()+daylightSaving()-28800000);
   break ;
  case 1: /*MST*/
   x.setTime(x.getTime()+daylightSaving()-25200000);
   break ;
  case 2: /*CST*/
   x.setTime(x.getTime()+daylightSaving()-21600000);
   break;
  case 3: /*EST*/
   x.setTime(x.getTime()+daylightSaving()-18000000);
   break;
   }
  
   return x;
}
function timeNow(){
   return new Date();
}
function daylightSaving(){
   return ((timeNow().getTime()>findDay(0,3,1,1).getTime())&&(timeNow().getTime()<findDay(0,9,1,-1).getTime()))?3600000:0;
}
function findDay(d,m,h,p){
   var week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date(timeNow().getUTCFullYear(),nm,1,h,0,0),dOff=0;
   if(p<0){
      x.setTime(x.getTime()-86400000);
   }
   if(x.getDay()!=d){
      dOff=(x.getDay()<d)?(d-x.getDay()):0-(x.getDay()-d);
      if(p<0&&dOff>0){
         week-=7;
      }
      if(p>0&&dOff<0){
         week+=7;
      }
      x.setTime(x.getTime()+((dOff+week)*86400000));
   }
   return x;
}
function leadingZero(x){
   return (x>9)?x:'0'+x;
}
function twelveHour(x){
   if(x==0){
      x=12;
   }
   return (x>12)?x-=12:x;
}
function dateEnding(x){
   if(x==1||x==21||x==31){
      return 'st';
   }
   if(x==2||x==22){
      return 'nd';
   }
   if(x==3||x==23){
      return 'rd';
   }
   return 'th';
}
function displayTime(){
   document.getElementById('tPST').innerHTML=eval(outputTimePST);
   document.getElementById('tMST').innerHTML=eval(outputTimeMST);
   document.getElementById('tCST').innerHTML=eval(outputTimeCST);
   document.getElementById('tEST').innerHTML=eval(outputTimeEST);
   setTimeout('displayTime()',1000);
}
function amPMsymbol(x){
   return (x>11)?'pm':'am';
}
function fixYear4(x){
   return (x<500)?x+1900:x;
}
var dayNames=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var monthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var outputTimePST="dayNames[timeSource(0).getDay()]+' '+timeSource(0).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(0).getMonth()]+' '+fixYear4(timeSource(0).getYear())+' '+':'+':'+' '+twelveHour(timeSource(0).getHours())+':'+leadingZero(timeSource(0).getMinutes())+':'+leadingZero(timeSource(0).getSeconds())+amPMsymbol(timeSource(0).getHours())";
var outputTimeMST="dayNames[timeSource(1).getDay()]+' '+timeSource(1).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(1).getMonth()]+' '+fixYear4(timeSource(1).getYear())+' '+':'+':'+' '+twelveHour(timeSource(1).getHours())+':'+leadingZero(timeSource(1).getMinutes())+':'+leadingZero(timeSource(1).getSeconds())+amPMsymbol(timeSource(1).getHours())";
var outputTimeCST="dayNames[timeSource(2).getDay()]+' '+timeSource(2).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(2).getMonth()]+' '+fixYear4(timeSource(2).getYear())+' '+':'+':'+' '+twelveHour(timeSource(2).getHours())+':'+leadingZero(timeSource(2).getMinutes())+':'+leadingZero(timeSource(2).getSeconds())+amPMsymbol(timeSource(2).getHours())";
var outputTimeEST="dayNames[timeSource(3).getDay()]+' '+timeSource(3).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(3).getMonth()]+' '+fixYear4(timeSource(3).getYear())+' '+':'+':'+' '+twelveHour(timeSource(3).getHours())+':'+leadingZero(timeSource(3).getMinutes())+':'+leadingZero(timeSource(3).getSeconds())+amPMsymbol(timeSource(3).getHours())";
if(!document.all){ window.onload=displayTime; }else{ displayTime(); }




Reference:
// Clock Script Generated By Maxx Blade's Clock v2.0d
// http://www.maxxblade.co.uk/clock

No comments: