-4

Possible Duplicate:
How can I get the DateTime for the start of the week?

I need to get start date of a week based on a input date and period start DayOfWeek. The week may start either from sunday or monday or etc.

//Ger start date based on a date and startdayofweek
GetStartDate(DateTime date, DayOfWeek periodStartDayOfWeek)
{
 // return startdateof week
}
Community
  • 1
  • 1
user1999616
  • 61
  • 1
  • 9
  • Is there a particular piece of code you're having trouble with, or are you asking us for a full example? – J. Steen Jan 22 '13 at 09:57

1 Answers1

0

What about this?

while (date.DayOfWeek != periodStartDayOfWeek)
{
    date = date.AddDays(-1);
}

return date;
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • 1
    While it is possible to do this without using a loop, given how often people **** it up, this is probably the safest solution. – Rawling Jan 22 '13 at 10:00