Get Day Of Year - CSharp System

CSharp examples for System:DateTime

Description

Get Day Of Year

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from  w w  w  . j a  va2 s  . com

public class Main{
        public static int GetDayOfYear(int year, int month, int day)
        {
            if (month < 1 || month > 12)
                return -1;

            int[] daysInMonths;

            int daysInYear = GetDaysInYear(year);
            if (daysInYear == 365)
                daysInMonths = daysInCommonYearMonths;
            else
                daysInMonths = daysInLeapYearMonths;

            int currentMonth = 1;
            int doy = 0;
            while (currentMonth < month)
            {
                doy += daysInMonths[currentMonth - 1];
                currentMonth++;
            }

            doy += day;
            if (doy > daysInYear)
                return daysInYear;
            else
                return doy;
        }
}

Related Tutorials