Home
Manage Your Code
Snippet: Sunday of the week (C#)
Title: Sunday of the week Language: C#
Description: Given a date, returns the Sunday for the week where a week is Sunday through Saturday. Views: 463
Author: Michael Eaton Date Added: 11/4/2007
Copy Code  
1private DateTime getSunday(DateTime fromDate) {
2    DateTime sunday = fromDate;
3    if (fromDate.DayOfWeek != DayOfWeek.Sunday) {
4	DateTime temp;
5	int dow = (int)fromDate.DayOfWeek;
6	temp = fromDate.AddDays(-dow);
7	if (temp.DayOfWeek == DayOfWeek.Sunday) {
8	    sunday = temp;
9	}
10    }
11    return sunday;
12}
13
14
Usage
// November 11 is a Wednesday
DateTime wed = new System.DateTime(2007, 11, 7);
Console.WriteLine(getSunday(wed));
Notes
Easy enough to modify for any other day of the week.