Faraz Shah Khan

MCP, MCAD.Net, MCSD.Net, MCTS-Win, MCTS-Web, MCPD-Web

Javascript to display time on Web page

Javascript to display continuous time on the Web page. By continuous it means that it will be displayed second by second.

    <script type="text/javascript">
        function ShowTime()
        {
            var dt = new Date();
            document.getElementById("<%= TextBox1.ClientID %>").value = dt.toLocaleTimeString();
            window.setTimeout("ShowTime()", 1000);
        }
       
            
    </script>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


If you are using normal Webform then it can be called on Body load event. If you are using MasterPage then it can be called within ContentTemplate at the end afte all the controls will be rendered.

Comments

kinkar Nag said:

Answer Me

# March 10, 2008 3:05 AM

Mike said:

Does this code run from the server, or from the viewer's local computer?

# July 23, 2008 5:02 PM

farazsk11 said:

The above code will run on the client side and will display the local time of the client side not the server side.

# July 24, 2008 1:57 AM

rocky said:

great solution...

its a recursive call to the same function...good one.

# April 27, 2009 4:17 AM

fijirap said:

This code produced the server time or the client system time ?...Please tell me...

# December 24, 2009 1:19 AM

farazsk11 said:

@fijirap:

As I mentioned in my earlier reply that it is a client side script and will display client side time.

# December 27, 2009 4:52 AM

Paul said:

I tried this code in a my View Content holder of my ASP.NET MVC page. It worked for a while and after a cople of second the page hanged on.

In addition, the compiler requested the TextBox to be included in Form tags like this;

<form id="form1" runat="server">

       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

   </form>

Please explain me what should be the problem with my MVC page.

Thanks,

  Paul

# February 11, 2010 3:21 PM

Paul said:

Hi,

I had an extra setTimeout call.

The code works fine.

Regards,

  Paul

# February 11, 2010 6:05 PM

Joe said:

How can I show the current time (continously) in a different time zone?

# October 7, 2010 3:44 PM

Sam said:

I need the source code to display the current time in UK, Nigeria  and Ghana on my web page.

# March 21, 2011 6:49 AM

Neil said:

I have a master page where I want to display time (continuous) in a label, which is outside content placeholder.

I have

<asp:Label ID="lblTime" runat="server" Text="00:00:00:00" Font-Bold="True" BackColor="#FFFF99">

</asp:Label>

and JavaScript as internal.

I'm calling ShowTime() at the onload event of master page body.

It is not displaying or updating the time.

Please help

# May 21, 2012 11:19 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)