I've been doing some ASP.NET development work in my spare time and came
across a customer requirement where the Calendar control used for a
reporting system needed to be able to:
- change the day that the
week started on based on a stored text value
- select all days from the
current week from today's date back to and including the starting day
of the week (whatever that was configured to)
The use of
different enumerations for Day surprised me and also the looping logic
required took some thinking about when the comparitive integer values of
Days could not be used as a means of comparison.
Screenshot and code sample below ran for today's date.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebCalendar
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
_cal.SelectionMode = CalendarSelectionMode.DayWeek;
// setup today's date values
DateTime dtCurrent = DateTime.Now;
DayOfWeek dowCurrent = dtCurrent.DayOfWeek;
// setup day to start week
string strDayToStartWeek = "Thursday"; // test text value - TODO: from database
DayOfWeek dowDayToStartWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDayToStartWeek);
FirstDayOfWeek configLeadingDOW = (FirstDayOfWeek)Enum.Parse(typeof(FirstDayOfWeek), strDayToStartWeek);
// set calendar to start week on stored preference
_cal.FirstDayOfWeek = configLeadingDOW;
// select the dates
SelectDatesForWeeklyReport(dtCurrent, dowDayToStartWeek);
}
/// <summary>
/// Select all the days from today back to the day which is nominated as the beginning of the week
/// </summary>
/// <param name="dtCurrent">Today's date</param>
/// <param name="dowDayToStartWeek">Day which is nominated as the beginning of the week</param>
private void SelectDatesForWeeklyReport(DateTime dtCurrent, DayOfWeek dowDayToStartWeek)
{
DateTime dtSelectedDate = dtCurrent;
bool exitLoop = true;
while (exitLoop)
{
if (dtSelectedDate.DayOfWeek == dowDayToStartWeek)
{
// select the start of the week
_cal.SelectedDates.Add(dtSelectedDate);
exitLoop = false;
}
else
{
_cal.SelectedDates.Add(dtSelectedDate);
}
dtSelectedDate = dtSelectedDate.AddDays(-1);
}
}
}
}