0

i have a drop down "Weeks". by default i want it to show the current week. i have tried using this

 for (int i = 1; i <= 5; i++)
 {

    ddlWeek.SelectedValue = ((DateTime.Now.Day + 6 / 7 ).ToString());
 }

but the problem is from "29/12/14 - 31/12/14" it is showing week 5. but when its "1/1/15" it is showing week 1 which i think is quite incorrect. my expected result is

  29/12/14 - 4/1/15 = "week 5",  
  5/1/15 - 11/1/15 = "week 1", 
  12/1/15 - 18/1/15 = "week 2",
  19/1/15 - 25/1/15 = "week 3",
  26/1/15 - 1/2/15 = "week 4",
  2/2/15 - 8/2/15 = "week 1"

and so on. how to i do so that the result appear like this??

SKJ
  • 37
  • 1
  • 10

1 Answers1

1

this code maybe can help you: I modify the code in the response of this post: Get a list of weeks for a year - with dates

You only need a DropDownList called DropDownList1 and paste the code in Page_Load event to test it.

        var jan1 = new DateTime(DateTime.Today.Year, 1, 1);

        var startOfFirstWeek = jan1.AddDays(1 - (int)(jan1.DayOfWeek));
        var weeks =
            Enumerable
                .Range(0, 54)
                .Select(i => new
                {
                    weekStart = startOfFirstWeek.AddDays(i * 7)
                })
                .TakeWhile(x => x.weekStart.Year <= jan1.Year)
                .Select(x => new
                {
                    x.weekStart,
                    weekFinish = x.weekStart.AddDays(6)
                })
                .SkipWhile(x => x.weekFinish < jan1.AddDays(1))
                .Select((x, i) => new
                {
                    x.weekStart,
                    x.weekFinish,
                    weekNum = i + 1
                });


        //After get weeks' information, manipulate it to get the results.
        DateTime Today = Convert.ToDateTime(DateTime.Now.Date.ToShortDateString()); //Convert to ShortDate to delete the HH:mm:ss
        string lblItem = "";

        foreach (var X in weeks)
        {
            //Check the Month and the Year must be from the actual Month
            if (X.weekStart.Month == Today.Month || X.weekFinish.Month == Today.Month && X.weekFinish.Year == Today.Year)
            {
                lblItem = "#" + X.weekNum + " " + X.weekStart.ToShortDateString() + " - " + X.weekFinish.ToShortDateString();
                DropDownList1.Items.Add(lblItem); //Add item in DropDownList
                if (Today >= X.weekStart && Today <= X.weekFinish) //Check if the current date is between the week
                    DropDownList1.SelectedValue = lblItem; //Select the current week                 
            }
        }

if you want to add the items with values manually, you can do this. And then compare the items with the current date, as i do in the code.

Hope this help.

Community
  • 1
  • 1
Mariachi
  • 139
  • 4
  • hai @TI-M. its working but it produces its own list of weeks from 1-53. i just want the weeks within the month. like the example that i showed in the question. how do i adjust that? – SKJ Jan 12 '15 at 08:32
  • hai @TI-M. it is showing "exception was unhandled by user code" in my catch (Exception ex) { throw ex}. can i know why?? – SKJ Jan 12 '15 at 14:52
  • Well, the code function OK for me. Could you give me more information about the problem? – Mariachi Jan 12 '15 at 19:58
  • hai @TI-M. i have solve the problem. but i have another problem. first, in my drop down list, it is only displaying from week 1-4, so when add a task on my calendar with the date "30-12-14", it is not displaying. Secondly, my first week is starting from "1-1-2015", but i want my first week to start on "5-1-2015". what i must adjust to get it right?? thanks. – SKJ Jan 13 '15 at 02:24
  • With the code simple i have this results: http://puu.sh/erGjB/2918b9bb9e.png do you need this: http://puu.sh/erHd3/8cb7f1475e.png ? – Mariachi Jan 13 '15 at 03:07
  • hai @tTI-M. I think puu.sh/erHd3/8cb7f1475e.png image is what i want. can you please help me? – SKJ Jan 13 '15 at 03:19
  • I change my answer please test the new code, i only change the first "if" condition in "foreach". – Mariachi Jan 13 '15 at 03:26
  • hai @TI-M. How can i change the spacing of the letters? because it is affecting all my drop down. the spacing between "week 1", "week 2" is very wide. thanks and sorry. – SKJ Jan 13 '15 at 08:35