home »forums »asp.net topics »client side web development »Display clock in textbox in javascript

Display clock in textbox in javascript

Topic RSS Feed

Posts under the topic: Display clock in textbox in javascript

Posted: 11/28/2009 9:18:58 AM

Lurker 170 points Lurker
  • Joined on: 10/17/2009 1:28:08 AM
  • Posts: 34

I have a textbox in asp.net page i want to show clock continiously in that textbox.


Posted: 11/29/2009 11:51:46 AM

Professional 9529 points Professional
  • Joined on: 4/19/2009 1:46:52 AM
  • Posts: 219
answered  Answered

In ASPX

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
<form runat="server">
    <asp:TextBox ID="txtClock" runat="server" style="width:400px;"></asp:TextBox>
</form>    
</body>

<script type="text/javascript">

    //Users time
    var timeLocal = new Date();

    //Calculate the difference (returns milliseconds)
    millDiff = timeLocal - timeServer;

    //initalize the clock on loading of page
    window.onload = function()
    {
        //set the interval so clock ticks
        var timeClock = setInterval('TimeTick()', 10);
    }

    //The ticking clock function
    function TimeTick()
    {
        //grab updated time
        timeLocal = new Date();
        //add time difference
        timeLocal.setMilliseconds(timeLocal.getMilliseconds() - millDiff);
        //display the value
        document.getElementById('<%= txtClock.ClientID %>').value = timeLocal;
    }

</script>

</html>


In Codebehind:

using System;

namespace TestProject
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(string.Format("<script>var timeServer = new Date('{0}');</script>", DateTime.Now));

        }

    }

}



Page 1 of 1 (2 items)