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.
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>
<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>

- How to get days between two DateTimes
- How to subtract two DateTimes
- How to get day from a DateTime
- How to get the current time of day
- How to check whether the given year is a leap year
- How to get system DateTime
- How to add 1 month to a given DateTime object
- How to get the first day of a month using a given date
- How to get the last day of month using a given date
- How to get the month name from month number