Get the date string representation of a given day (e.g. "2004.02.15") - CSharp System

CSharp examples for System:DateTime

Description

Get the date string representation of a given day (e.g. "2004.02.15")

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*ww w.jav  a 2  s . c o m*/

public class Main{
        /// <summary>
        /// Get the date string representation of a given day (e.g. "2004.02.15")
        /// </summary>
        /// <param name="year"></param>
        /// <param name="dayOfYear"></param>
        /// <returns></returns>
        public static string GetDateString(int year, int dayOfYear)
        {
            int daysInYear = GetDaysInYear(year);
            if (dayOfYear > daysInYear)
                return null;

            int[] daysInMonths;
            if ( daysInYear == 365)
                daysInMonths = daysInCommonYearMonths;
            else
                daysInMonths = daysInLeapYearMonths;

            int month = 1;
            int remainedDays = dayOfYear;
            while (remainedDays > daysInMonths[month-1])
            {
                remainedDays -= daysInMonths[month-1];
                month++;
            }

            return year + "." + GetTwoDigitString(month) + "." + GetTwoDigitString(remainedDays);
        }
}

Related Tutorials