UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Get current time in Asp.net -C#

Demo and Code for how to get current time and UTC time in ASP.net - c#

by Athil


Posted on 28 Jul 2017 Category: Asp.net Views: 831

Edited on 25 Jul 2018


DEMO

Hi, Here I am going to show how to get the current time and current UTC time in asp.net using c#.

To get current time

 TextBox1.Text = DateTime.Now.ToShortTimeString();

To get current UTC time

 TextBox1.Text = DateTime.UtcNow.ToShortTimeString();

 

Full Demo code for getting current time and UTC time

on client side

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo for get Current time and current UTC time in asp.net - C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Demo for get Current time and current UTC time in asp.net - C# </h1>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br/>
        <asp:Button ID="Button1" runat="server" Text="get time" OnClick="Button1_Click" /> <br/>
          <asp:Button ID="Button2" runat="server" Text="get UTC time" OnClick="Button2_Click" />
    </div>
    </form>
</body>
</html>

On server side 

protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox1.Text = DateTime.Now.ToShortTimeString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
               TextBox1.Text = DateTime.UtcNow.ToShortTimeString();
        }

 

 



Leave a Comment:


Advertisement