c# - How to get system DateTime

Get system DateTime
DateTime.Now property allow us to get a System DateTime object that is set to the current date and time on this computer (web server). this property exists in System namespace. this property return value type is System.DateTime which represent the current local date and time. after getting the current datetime object we can format the date and time to display in web page.

the following asp.net c# example code demonstrate us how can we get system date time programmatically at run time in an asp.net application.
Now.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        DateTime now = DateTime.Now;
        Label1.Text = "System DateTime[Now]: " + now.ToString();
        Label1.Text += "<br />System DateTime[ToDay]: " + now.ToLongDateString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net date time example: how to get system date time</title>
</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">asp.net example: DateTime.Now</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="PaleVioletRed"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Get System DateTime[Now]"
             ForeColor="Magenta"
             />   
    </div>
    </form>
</body>
</html>
More c# examples