Friday 6 December 2013

How to Get All the Weeks into Drop Down list using C# .NET

                                  
Today I am going to explain how we can get all the weeks of a year into Dropdown list using C#.NET
In our application sometimes we may have a requirement to show all the weeks in the dropdown list. We can use C# .NET to get this functionality.

First declare a dropdown list in aspx page as follows

<table>
            <tr>
                <td>
                    <asp:DropDownList ID="ddlWeeks" runat="server" AutoPostBack="true">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>

Now write the code to bind the values in aspx.cs page

First take a method. Here I have declared a method called GetWeeks()

public void GetWeeks()
        {
            try
            {

      }
            catch (Exception ex)
            {

            }
        }



Now in get weeks declare two lists.

To get all the weeks we need to know the current year. To get the current year, declare a datetime variable.

DateTime CurrentTime = System.DateTime.Now;

Now in the variable CurrentTime, we’ll have the current year, month, day, day of week etc.

Now we’ll select the current year from the above datetime variable.

int CurrentYear = CurrentTime.Year;

In this way we can get the current year.

Undeniably every year starts with January 1st. Now we’ll pass this value to get the starting day of the year.

Declare another datetime variable and pass these values into the variable.

DateTime CurrentDate = new DateTime(CurrentYear, 01, 01);

In new DateTime

The CurrentYear describes the current year.

First 01 represents the month.

Second 01 represents the date.

We’ll pass these integer values into the variable as shown above.

Now we need to check the day of 1st January.

To do this we have write a string variable and check the day. As we have already passed the variables using CurrentDate we can determine the day of 1st January.

Declare a string variable called CurrentDay.

Pass the day value into the current date.

string CurrentDay = CurrentDate.DayOfWeek.ToString();

Generally there are five working days in a week. So we’ll show these five working days in the drop down list.

To determine the first day of the year we’ll use if condition.

We will check whether the current day is Monday or not. If the first day is Monday then the week begins with Monday.

if (CurrentDay == DayOfWeek.Monday.ToString())
{
beginingDate = new DateTime(CurrentYear, 01, 01);
}

If the beginning day is Monday then the week directly begins with 01 and ends with 05. But January 1st may fall on other days of the week. For example, if January 1st is on Thursday then the week had begun in December itself. So we have to determine the day on which January 1st falls. If January 1st is on Tuesday then the week begins with 31st December. Else if January 1st is on Wednesday means the week begins on January 30th December. In order to determine these we’ll write the following conditions.

else if (CurrentDay == DayOfWeek.Tuesday.ToString())
{
beginingDate = new DateTime(CurrentYear - 1, 12, 31);
}
else if (CurrentDay == DayOfWeek.Wednesday.ToString())
{
beginingDate = new DateTime(CurrentYear - 1, 12, 30);
}
else if (CurrentDay == DayOfWeek.Thursday.ToString())
{
beginingDate = new DateTime(CurrentYear - 1, 12, 29);
}
else if (CurrentDay == DayOfWeek.Friday.ToString())
{
beginingDate = new DateTime(CurrentYear - 1, 12, 28);
}
else if (CurrentDay == DayOfWeek.Saturday.ToString())
{
beginingDate = new DateTime(CurrentYear, 01, 03);
}
else if (CurrentDay == DayOfWeek.Sunday.ToString())
{
beginingDate = new DateTime(CurrentYear, 01, 02);
}

Now we’ll know on which day the week has started.

We’ll increment the current year by one to get the next year value. Declare another datetime variable and assign this value.

DateTime endDate = new DateTime(CurrentYear + 1, 01, 01);

We’ll write a while loop and get all the weeks ranging from beginning of the current year to beginning of the next year.

while (beginDate < endDate)
{
if (beginDate.DayOfWeek == DayOfWeek.Monday)
{
monday = beginDate;
}
else if (beginDate.DayOfWeek == DayOfWeek.Friday)
{
friday = beginDate;
}
else if (beginDate.DayOfWeek == DayOfWeek.Saturday)
{
weeks.Add(new DateTime[] { monday, friday });
}
beginDate = beginDate.AddDays(1);
}

We’ll bind all the values to the drop down list.
Call this method in the pageload event

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetWeeks();
            }
        }

The entire code altogether is

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetWeeks();
            }
        }
        public void GetWeeks()
        {
            try
            {
                List<DateTime[]> weeks = new List<DateTime[]>();
                List<string> strweeks = new List<string>();
                DateTime CurrentTime = System.DateTime.Now;
                DateTime beginingDate = new DateTime();
                int CurrentYear = CurrentTime.Year;
                DateTime CurrentDate = new DateTime(CurrentYear, 01, 01);
                string CurrentDay = CurrentDate.DayOfWeek.ToString();
                if (CurrentDay == DayOfWeek.Monday.ToString())
                {
                    beginingDate = new DateTime(CurrentYear, 01, 01);
                }
                else if (CurrentDay == DayOfWeek.Tuesday.ToString())
                {
                    beginingDate = new DateTime(CurrentYear - 1, 12, 31);
                }
                else if (CurrentDay == DayOfWeek.Wednesday.ToString())
                {
                    beginingDate = new DateTime(CurrentYear - 1, 12, 30);
                }
                else if (CurrentDay == DayOfWeek.Thursday.ToString())
                {
                    beginingDate = new DateTime(CurrentYear - 1, 12, 29);
                }
                else if (CurrentDay == DayOfWeek.Friday.ToString())
                {
                    beginingDate = new DateTime(CurrentYear - 1, 12, 28);
                }
                else if (CurrentDay == DayOfWeek.Saturday.ToString())
                {
                    beginingDate = new DateTime(CurrentYear, 01, 03);
                }
                else if (CurrentDay == DayOfWeek.Sunday.ToString())
                {
                    beginingDate = new DateTime(CurrentYear, 01, 02);
                }
                DateTime beginDate = beginingDate;
                DateTime endDate = new DateTime(CurrentYear + 1, 01, 01);
                DateTime monday = DateTime.Today;
                DateTime friday = DateTime.Today;
                while (beginDate < endDate)
                {
                    if (beginDate.DayOfWeek == DayOfWeek.Monday)
                    {
                        monday = beginDate;
                    }
                    else if (beginDate.DayOfWeek == DayOfWeek.Friday)
                    {
                        friday = beginDate;
                    }
                    else if (beginDate.DayOfWeek == DayOfWeek.Saturday)
                    {
                        weeks.Add(new DateTime[] { monday, friday });
                    }
                    beginDate = beginDate.AddDays(1);
                }
                for (int x = 0; x < weeks.Count; x++)
                {
                    strweeks.Add(weeks[x][0].Date.ToShortDateString() + " - " + weeks[x][1].Date.ToShortDateString());
                }
                ddlWeeks.DataSource = strweeks;
                ddlWeeks.DataBind();
            }
            catch (Exception ex)
            {

            }
        }

Output Screen Shot



2 comments:

  1. Hi Buddy,

    If i mention month and year, in a dropdownlist list of weeks should be display in the form of firstdate of week - lastdate of week.

    Please help me out of this.

    Thank You,

    Arjun.

    ReplyDelete
  2. How do you get all 7 days and not jus 5 days?

    ReplyDelete