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.

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

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
            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;

<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>
    <form id="form1" runat="server">
        <h2 style="color:Red">asp.net date time example: DayOfYear</h2>
        <br /><br />
More c# examples