Hi,

I have a problem accessing server time using javascript. I'm making a home page in asp and want to access server time using javascript. What i'm doing presently is accessing server time in vbscript, and now want to display this in a javascript calendar which i hav already made. Now the thing left is displaying time and also refreshing it without refreshing whole page. Can any body help. I know how to do for SSI and PHP pages. But want to try in ASP page. Can nybody suggest???

Hi,

You will need to create a clock in javascript that will refresh the time.

//Code behind

protected void Page_Load(object sender, EventArgs e)
    {
[INDENT]const string crlf = "\r\n";
str[INDENT][/INDENT]ing jsscript = "<script type='text/javascript'>" + crlf +
            "window.onload=startclock;" + crlf +
            "var clock;" + crlf +
            "var time_diff;" + crlf +
            "function startclock(){" + crlf +
               "clock=document.getElementById('clockLocal');" + crlf +
               "server_time = new Date('" + DateTime.Now.ToString("MMMM d, yyyy HH:mm:ss") + "');" + crlf +
               "time_diff=new Date()-server_time;" + crlf +
               "setInterval('runclock()',1000);" + crlf +
            "}" + crlf +
            "function runclock(){" + crlf +
               "var cDate=new Date();" + crlf +
               "cDate.setTime(cDate.getTime()-time_diff);" + crlf +
               "var curr_hours = cDate.getHours();" + crlf +
               "var curr_mins = cDate.getMinutes();" + crlf +
               "var curr_secs = cDate.getSeconds();" + crlf +
               "curr_hours=(curr_hours < 10)?'0' + curr_hours:curr_hours;" + crlf +
               "curr_mins=(curr_mins < 10)?'0' + curr_mins:curr_mins;" + crlf +
               "curr_secs=(curr_secs < 10)?'0' + curr_secs:curr_secs;" + crlf +
               "clock.innerHTML=curr_hours+':'+curr_mins+':'+curr_secs;" + crlf +
               "}" + crlf +
            "</script>";[/INDENT]
       Page.RegisterStartupScript("ServerTimeScript", jsscript );

    }

//ASPX page

<body onload="startclock();">
<span id=clockLocal style="position:relative;"></span>
This article has been dead for over six months. Start a new discussion instead.