Sundays the date. - CSharp System

CSharp examples for System:DateTime Day

Description

Sundays the date.

Demo Code

// Copyright by the Spark Development Network
using System;// w ww  . j a  v a  2  s  . c o  m

public class Main{
        /// <summary>
        /// Sundays the date.
        /// </summary>
        /// <param name="dt">The date to check.</param>
        /// <param name="startOfWeek">The start of week.</param>
        /// <returns></returns>
        public static DateTime SundayDate( this DateTime dt, DayOfWeek startOfWeek = DayOfWeek.Monday )
        {
            if ( dt.DayOfWeek == DayOfWeek.Sunday )
            {
                return dt.Date;
            }
            else
            {
                int intDayofWeek = (int)dt.DayOfWeek;
                int diff = 7 - (int)dt.DayOfWeek;
                return dt.AddDays( diff ).Date;
            }
        }
}

Related Tutorials