Day Of Year
This algorithm finds the number of current day in a year according to the input date.
public static int DayOfYear(uint year, byte month, byte day)
{
ushort[] days = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
if (IsLeapYear(year) && month >= 2)
return days[month - 1] + day + 1;
return days[month - 1] + day;
}
private static bool IsLeapYear(uint year)
{
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
Example
int value = DayOfYear((uint)DateTime.Now.Year, (byte)DateTime.Now.Month, (byte)DateTime.Now.Day);
Output
271