GetSunday

public static DateTime GetSunday(this DateTime dt)
        {
            DateTime date = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
            return new GregorianCalendar().AddDays(date, -((int)date.DayOfWeek));
        }
Example:
DateTime oldDate = new DateTime(2017,5,5);
DateTime Sunday = oldDate.GetSunday();

results in a value of 2017-April-30 which is correct in this instance.

Description

This code will provide the Sunday DateTime from the week of DateTime object the extension method is called from.

Details

Double click on the code to select all.

 

;