Thursday, October 26, 2006

Content Editor Webpart Current Date time in sharepoint

copy paste the code in the 'source editor' of a content editor webpart:


<SPAN id=tP> </SPAN> <SCRIPT type=text/javascript>function tS(){ x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()+dS()+34200000); return x; } function tN(){ return new Date(); } function dS(){ return ((tN().getTime()>fD(0,3,1,1).getTime())&&(tN().getTime()<fD(0,9,1,-1).getTime()))?3600000:0; } function fD(d,m,h,p){ var week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date(tN().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 lZ(x){ return (x>9)?x:'0'+x; } function tH(x){ if(x==0){ x=12; } return (x>12)?x-=12:x; } function y2(x){ x=(x<500)?x+1900:x; return String(x).substring(2,4) } function dT(){ window.status=''+eval(oT)+'';  document.getElementById('tP').innerHTML=eval(oT); setTimeout('dT()',1000); } function aP(x){ return (x>11)?'pm':'am'; } var dN=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'),mN=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),oT="dN[tS().getDay()]+' '+tS().getDate()+' '+mN[tS().getMonth()]+' '+y2(tS().getYear())+' '+':'+':'+' '+tH(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+aP(tS().getHours())";if(!document.all){ window.onload=dT; }else{ dT(); }</SCRIPT>

2 comments:

  1. Hi, Thanks for this code. Unfortunately it displays the time 30 mins earlier than the UTC set on the server - do you know why.

    Regards
    David

    ReplyDelete
  2. how do we change the time difference. My time zone is GMT +3 Riyadh Saudi Arabia..

    Above code works fine but there is difference in time

    ReplyDelete