Wednesday, September 14, 2011

Update server date time at user side by JavaScript

Hello. u can update server time at user side with different ways. I have done this as following:
On refresh I get server date time as milliseconds:



var sweDateTimeInMillis = <%= TimeStampCustom.getCurrentDateTimeInMillis() %>;
showSWEDateTime(sweDateTimeInMillis);
and pass it to my javascript function as parameter. Function looks like as following:
var Dom = YAHOO.util.Dom;
var YUIDate = YAHOO.util.Date;

function showSWEDateTime(sweDateTimeInMillis)
{ 
 var sweDate = new Date(sweDateTimeInMillis); 

 Dom.get('ClockTime').innerHTML = YUIDate.format(sweDate, {format: "%d-%B-%G %A, %H:%M"}, "en-US");
 
 sweDateTimeInMillis += 1000;
 
 t=setTimeout("showSWEDateTime("+sweDateTimeInMillis+")",1000);
}

Here setTimeout() - standard JS method which executes a code some time in the future. (for more information click here)

No comments:

Post a Comment