Try this:
private DateTime GetMondayOfWeek(DateTime dt)
{
CultureInfo myCI = new CultureInfo("en-US");
// Get the day of the week from the input.
DayOfWeek dow = myCI.Calendar.GetDayOfWeek(dt);
// Get the difference between that day and Monday
// (it might be negative if Monday is not the
// first day of the week)
int diff = dow - DayOfWeek.Monday;
// Correct if it's negative.
if (diff < 0)
diff += 7;
// Get the preceding Monday.
DateTime dtMonday = dt.AddDays(-diff).Date;
return dtMonday;
}
Tom Dacon
Dacon Software Consulting
"Frank Rizzo" <no**@none.netwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
>I need to get a Monday of the current week based on any date. I got the
function below working fine, but it feels hacky. And I can't find anything
in the framework. Is there a more elegant solution?
private DateTime GetMondayOfWeek(DateTime dt)
{
CultureInfo myCI = new CultureInfo("en-US");
while (myCI.Calendar.GetDayOfWeek(dt) != DayOfWeek.Monday)
dt = dt.AddDays(-1);
return dt;
}