Display Current time in webpage

In this blog we will know how to display the running time in web page.

 

 

Method-1

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Show_time_in_webpage._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

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

    <div>

    <asp:ScriptManager ID="ScriptManager1"

            runat="server">

        </asp:ScriptManager>

<asp:Timer ID="Timer1" runat="server" Interval="100" ontick="Timer1_Tick">

                </asp:Timer>

        <asp:UpdatePanel ID="UpdatePanel1"

            runat="server">

            <ContentTemplate>

            <asp:Label ID="Label2"  Text="Current Time:" runat="server" Font-Bold="True"

                    ForeColor="#FF9900"></asp:Label>

                   <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="Maroon"></asp:Label>

                <br />

            </ContentTemplate>

            <Triggers>

                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"></asp:AsyncPostBackTrigger>

            </Triggers>

        </asp:UpdatePanel>

        <br />

    </div>

    </form>

</body>

</html>

 

 

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

namespace Show_time_in_webpage

{

    public partial class _Default : System.Web.UI.Page

    {

 

        protected void Timer1_Tick(object sender, EventArgs e)

        {

            Label1.Text = DateTime.Now.ToString("hh:mm:ss");

        }

    }

}

 

 

 

Method-2

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Show_time_in_webpage.WebForm1" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

    <script type="text/javascript">

 

    function ShowTime() {

 

        var dt = new Date();

 

        document.getElementById("<%= TextBox1.ClientID %>").value = dt.toLocaleTimeString();

 

        window.setTimeout("ShowTime()", 1000);

 

    } 

 

</script>

<script type="text/javascript">

 

window.setTimeout("ShowTime()", 1000);

 

</script>

</head>

<body>

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

    <div>

        <asp:Label ID="Label1" runat="server" Text="Current Time:"></asp:Label>

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

    </div>

    </form>

</body>

</html>

 

 

 

Thanks for reading

 

 

 

Ebook Download
View all
Learn
View all