Week Selection from Date Time Picker

This is a discussion on Week Selection from Date Time Picker within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I am using date time picker in my project in which I need to select weeks from this control ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    Smile Week Selection from Date Time Picker

    Hi, I am using date time picker in my project in which I need to select weeks from this control (just like weeks are selected in the top leftmost calendar and the corresponding dates are displayed in MS Outlook). Is there any way that I can retrieve the dates of the week from the date time picker when the user selects a week in date time picker.
    Any help would be appreciated. Thanks!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I don't think there's a clean way to do it. This seems to work well though:

    Code:
            private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
            {
                string[] days = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    
                DateTime selectedDate = dateTimePicker1.Value.Date;
                string weekDay = selectedDate.DayOfWeek.ToString();
                int index = -1;
                for(int i = 0;i < days.Length;++i)
                {
                    if(days[i] == weekDay)
                    {
                        index = i;
                        break;
                    }
                }
    
                if(index == -1)
                {
                    MessageBox.Show("I don't know WTF day " + weekDay + " is.", "Error");
                    return;
                }
    
                DateTime startDate = selectedDate - new TimeSpan(index, 0, 0, 0);
    
                DateTime[] weekDates = new DateTime[7];
                for (int i = 0; i < 7; ++i)
                    weekDates[i] = startDate + new TimeSpan(i, 0, 0, 0);
    
                StringBuilder sb = new StringBuilder();
                foreach (DateTime date in weekDates)
                    sb.Append(date + "\n");
                MessageBox.Show(sb.ToString());
            }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Well, thanks a lot. But I failed to mention that I am using pure 'C' for my project. It seems that I will have to think something else. Thanks, anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hey all for the second time this week
    By xixpsychoxix in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2007, 02:14 PM
  2. How to read from date and time picker?
    By vikernes in forum Windows Programming
    Replies: 3
    Last Post: 06-17-2006, 07:10 PM
  3. Date and Time
    By Calavera in forum C Programming
    Replies: 3
    Last Post: 11-26-2004, 02:56 PM
  4. Date and Time
    By fkheng in forum C Programming
    Replies: 19
    Last Post: 06-10-2003, 02:51 AM
  5. Debugging mode selection during compile time
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 09-03-2001, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21