Easy Way To Show Week Of Year And Corresponding Date For a Year in C#.Net

Here I put the simplest way to find the week of the year and also display the date and week in the form of [01] [01-Jan-2009]

CODE



public ArrayList findWeek()
{
DateTime date = DateTime.Now.AddDays(-(DateTime.Now.DayOfYear-1));
ArrayList arr=new ArrayList ();
int yr=date.Year;
while (date.Year!=yr+1)
{
string strdate = "[" + Convert.ToString((date.DayOfYear-1) / 7+1) + "] [" + Convert.ToString(date.Day + "-" + (date.Date.ToString("dd-MMM-yyyy").Split('-'))[1] + "-" + date.Year) + "]";
arr.Add(strdate);
date=date.AddDays(1);
}
return arr;
}

The above code return one ArrayList object which contains all date from 01 jan to 31 Dec of a current year.

You can put that arraylist into any control like dropdownlist or listbox like the following code :

DropDownList1.Items.Clear();
for (int i = 0; i < arr.Count; i++)
{
DropDownList1.Items.Add(arr[i].ToString());
}


Reference: http://msahoo.wordpress.com/2009/06/06/easy-way-to-show-week-of-year-and-corresponding-date-for-a-year-in-c-net/


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: