10

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 help me with this I'm new to C# and asp programming.

Michael Quiles
  • 1,131
  • 3
  • 24
  • 41

4 Answers4

31
DateTime startOfWeek = DateTime.Today.AddDays(-1 * (int)(DateTime.Today.DayOfWeek));

Regarding adding items to a DropDownList in ASP.NET, assuming you're using the webforms model, you would do something like

yourDropDown.Items.Add(new ListItem(yourText, yourValue));
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
4
DateTime startOfWeek = Today.AddDays((int)Today.DayOfWeek * -1)

Likely an off by one error, since it is untested.

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
  • It would not be off by one, it just wouldn't compile! – Anthony Pegram Jun 17 '10 at 15:02
  • 4
    That's what happens when a VB programmer tries to write C# code. :) – Jason Berkan Jun 17 '10 at 15:13
  • To get Sunday use: `DateTime startOfWeek = DateTime.Today.AddDays(-1 * (int)(DateTime.Today.DayOfWeek) - 1);` – Walter Mar 19 '15 at 01:11
  • @Walter, your revision will produce Saturday. – Anthony Pegram Mar 19 '15 at 02:40
  • Oops, it turns out that last Saturday and not last Sunday is what I needed, The code list above also had an off by one bug. Here is the updated code for last Saturday without the off by one bug `DateTime startOfWeek = DateTime.Today.AddDays(DateTime.Today.DayOfWeek == 0 ? 0 : (- 1 * (int)(DateTime.Today.DayOfWeek) - 1));` – Walter Mar 19 '15 at 18:47
1

Here's an example that uses an iterator.

    /// <summary>
    /// Gets the dates that mark the beginning of each week from the specified start date until today.
    /// </summary>
    /// <param name="weekStart">The day of the week that marks the beginning of a week</param>
    /// <param name="startDate">A date that determines the earliest week in the result.</param>
    /// <returns></returns>
    public IEnumerable<DateTime> 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<DateTime> GetWeeks(DayOfWeek weekStart, DateTime startDate)
    {
        List<DateTime> result = new List<DateTime>();

        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;
    }
Dr. Wily's Apprentice
  • 10,212
  • 1
  • 25
  • 27
0
DateTime.Now.Date.AddDays(-1 * (Int32)DateTime.Now.DayOfWeek)
decyclone
  • 30,394
  • 6
  • 63
  • 80