Finds the last day of the year for the selected day's year. - CSharp System

CSharp examples for System:DateTime Year

Description

Finds the last day of the year for the selected day's year.

Demo Code


using System.Globalization;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from w  ww  . j  a v a  2 s. co m

public class Main{
        /// <summary>
        /// Finds the last day of the year for the selected day's year.
        /// </summary>
        public static DateTime LastDayOfYear(DateTime d)
        {
            // 1
            // Get first of next year
            DateTime n = new DateTime(d.Year + 1, 1, 1);
            // 2
            // Subtract 1 from it
            return n.AddDays(-1);
        }
        /// <summary>
        /// Finds the last day of the year for today.
        /// </summary>
        public static DateTime LastDayOfYear()
        {
            return LastDayOfYear(DateTime.Today);
        }
}

Related Tutorials