c# - How to get day of year

Get day of year from DateTime
DateTime.DayOfYear property allow us to get the day of the year from a specified datetime object. this property exists in System namespace. this DateTime.DayOfYear property return value data type is System.Int32 which represent the day of the year, expressed as a value between 1 and 366. the day number depends on leap year.

the following asp.net c# example code demonstrate us how can we get the day of year from a datetime object programmatically at run time in an asp.net application.
DayOfYear.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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.SkyBlue;
            Label1.Font.Bold = true;
            Label1.Font.Italic = true;
            Button1.Font.Bold = true;
            Button1.ForeColor = System.Drawing.Color.DarkBlue;
            Button1.Text = "Get Day Of Year";
        }
    }
    
    protected void Button1_Click(object sender, System.EventArgs e) {
        DateTime today = DateTime.Now;
        string dayOfYear = today.DayOfYear.ToString();

        Label1.Text = "Today: " + DateTime.Now.ToLongDateString();
        Label1.Text += "<br />Day Of Year: " + dayOfYear;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net date time example: how to get the day of year from date time object in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net date time example: DayOfYear</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             />   
    </div>
    </form>
</body>
</html>
More c# examples