I\'m attempting to recreate a time sheet built on asp and I can\'t figure out how to get the current weeks starting date \"6-13-2010\" and have it populate a combo box can you h
Here's an example that uses an iterator.
///
/// Gets the dates that mark the beginning of each week from the specified start date until today.
///
/// The day of the week that marks the beginning of a week
/// A date that determines the earliest week in the result.
///
public IEnumerable GetWeeks(DayOfWeek weekStart, DateTime startDate)
{
DateTime current = DateTime.Today.AddDays(weekStart - DateTime.Today.DayOfWeek);
while(current >= startDate)
{
yield return current;
// move to the previous week
current = current.AddDays(-7);
}
}
public void PopulateUI()
{
// in this example, Monday is considered the start of the week,
// and the drop down list will be populated with the date of each monday starting with this week going back to 1 year ago.
ddlWeeks.DataSource = GetWeeks(DayOfWeek.Monday, DateTime.Today.AddYears(-1));
}
You mentioned that you're pretty new to C#, so sorry if it seems confusing. Below is an alternative GetWeeks function that is not defined as an iterator.
public List GetWeeks(DayOfWeek weekStart, DateTime startDate)
{
List result = new List();
DateTime current = DateTime.Today.AddDays(weekStart - DateTime.Today.DayOfWeek);
while (current >= startDate)
{
result.Add(current);
// move to the previous week
current = current.AddDays(-7);
}
return result;
}