Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current week starting date and add it to a combo box?

Tags:

c#

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.

like image 381
Michael Quiles Avatar asked Jun 17 '10 14:06

Michael Quiles


People also ask

How do I get the current week of the year in C#?

WriteLine("The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW) Console. WriteLine("Therefore, the current week is Week {0} of the current year.", myCal. GetWeekOfYear(DateTime. Now, myCWR, myFirstDOW)) ' Displays the total number of weeks in the current year.

How do I get a week start date and end date in VB net?

Here is the code by SGWellens: int NumWeeks = 30; DateTime StartDate, EndDate; DateTime BaseDate = new DateTime(2010, 1, 1); BaseDate = BaseDate. AddDays(NumWeeks * 7); StartDate = BaseDate; while (StartDate. DayOfWeek !=


3 Answers

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));
like image 69
Anthony Pegram Avatar answered Nov 04 '22 22:11

Anthony Pegram


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

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

like image 38
Jason Berkan Avatar answered Nov 04 '22 20:11

Jason Berkan


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;
    }
like image 1
Dr. Wily's Apprentice Avatar answered Nov 04 '22 21:11

Dr. Wily's Apprentice