Here is a quick way to get the first day of the current week.
If you are happy with sunday as the first day of the week, then you can simply do this :
public static DateTime FirstDayOfWeek(this DateTime date) { return DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek); }
But if you want that the first day of the week matches your culture (eg: the belgian week starts on monday), you have to take care of the culture’s DateTime format.
So the method will change a bit :
public static DateTime FirstDayOfWeek(this DateTime date) { return DateTime.Today.AddDays(-((int)DateTime.Today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)); }
Usage :
DateTime FirstDayOfWeek = DateTime.Today.FirstDayOfWeek();
or
DateTime FirstDayOfWeek = DateTime.Now.FirstDayOfWeek();
Incoming search terms:
- asp net get first day of week
- first day of week in asp net
- get first day of the week in linq
- jquery get first day of current week
- linq how to get the first day of a week
- net first day of week