UncleCoder.com

UncleCoder.com

Free programming examples and instructions

get current date in asp.net - c#

Demo and code for how to get current date and current UTC date tin asp.net c#

by Athil


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

Edited on 28 Jul 2017


DEMO

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

To get current Date

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

To get current UTC Date

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

 

Full Demo code for getting current date and UTC date

On client side

<!DOCTYPE html>

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

    </div>
    </form>
</body>
</html>

On server Side

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

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

 



Leave a Comment:


Advertisement