UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Get current date and time in Asp.net - c#

Demo and code for how to get current date and time using asp.net - c#.

by Athil


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

Edited on 28 Jul 2017



DEMO

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

To get current date and time

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

To get current UTC date and time

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

Full Demo code for getting current date and time

On Client Side

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo for get current date and time and UTC TIME</title>
</head>
<body>
    <h1>Demo for get current date and time and UTC TIME</h1>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btncurrentdatetime" runat="server" Text="Current date & time" OnClick="btncurrentdatetime_Click" />
        <br />
        <asp:Button ID="btnUTcdatetime" runat="server" Text="Current UTC date & time" OnClick="btnUTcdatetime_Click" />
        <br />
    </div>
    </form>
</body>
</html>



On Server Side

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

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

 



Leave a Comment:


Advertisement