Thursday, March 12, 2009

Get 1st day of week using C#.

Hi,

Here is the code to get date on 1st day of week, we just need to call this function with date:


private static DateTime firstDayOfWeek(DateTime day, DayOfWeek weekStarts)
{
DateTime d = day;
while (d.DayOfWeek != weekStarts)
{
d = d.AddDays(-1);
}

return d;
}


and to call this function:


DateTime myDate = firstDayOfWeek(DateTime.Now, DayOfWeek.Sunday);

No comments:

Post a Comment