c# - How to get day from a DateTime

Get day from DateTime
DateTime.Day property allow us to get the day of the month from a specified datetime object. this property exists in System namespace. the DateTime.Day property return value data type is System.Int32 which represent the day part of a datetime object. the property always return a value between 1 and 31.

the following asp.net c# example code demonstrate us how can we get day number from a date time object programmatically at run time in an asp.net application.
Day.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if(!this.IsPostBack)
        {
            Label1.Font.Size = FontUnit.Large;
            Label1.ForeColor = System.Drawing.Color.HotPink;
            Label1.Font.Bold = true;
            Label1.Font.Italic = true;
        }
    }
    
    protected void Button1_Click(object sender, System.EventArgs e) {
        DateTime myDateTime = DateTime.Now;
        string day = myDateTime.Day.ToString();

        Label1.Text = "My DateTime: " + DateTime.Now.ToLongDateString();
        Label1.Text += "<br />Day: " + day;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net date time example: how to get day from a date time object in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net date time example: Day</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             Font-Bold="true" 
             ForeColor="DarkOliveGreen" 
             OnClick="Button1_Click"
             Text="Get Day From DateTime Object"
             />   
    </div>
    </form>
</body>
</html>
More c# examples