GetSunday
This code will provide the Sunday DateTime from the week of DateTime object the extension method is called from.
Source
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.
Author: Brent Coppock (original by Charles Cherry)
Submitted on: 23 nov. 2017
Language: C#
Type: DateTime
Views: 4197