Share via


how to bind dropdown box with time interval

Question

Tuesday, October 16, 2012 9:46 PM

I am seeking a function to populate the dropdown list with time interval. For example, when user clicks the dropdown lis, there should be like :

8:00AM

8:30AM

9:00AM

....

12:00PM

12:30PM

All replies (12)

Wednesday, October 17, 2012 5:46 AM ✅Answered | 2 votes

You can make use of TimeSpan  class DateTime.ToShortTimeString() method to get the desired list. I have a function below that you can use to get the list you want. Populate your dropdown with this.

        public List<string> GetTimeIntervals()        {            List <string> timeIntervals = new List<string>();            TimeSpan startTime = new TimeSpan(0, 0, 0);                        DateTime startDate = new DateTime(DateTime.MinValue.Ticks); // Date to be used to get shortTime format.            for (int i = 0; i < 48; i++)            {                int minutesToBeAdded = 30 * i;      // Increasing minutes by 30 minutes interval                TimeSpan timeToBeAdded = new TimeSpan(0, minutesToBeAdded, 0);                TimeSpan t = startTime.Add(timeToBeAdded);                DateTime result = startDate + t;                timeIntervals.Add(result.ToShortTimeString());      // Use Date.ToShortTimeString() method to get the desired format                            }            return timeIntervals;        }

Thanks,

Sameer

If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.


Tuesday, October 16, 2012 9:52 PM

Hello sdns2000,

I am seeking a function to populate the dropdown list with time interval. For example, when user clicks the dropdown lis, there should be like :

8:00AM

8:30AM

9:00AM

....

12:00PM

12:30PM

if you mean a drop down list ComboBox control, you have to handle the tick event of the timer and set it in your case every half hour, so if a second and 1000 ms, a minute 60000, 60000 x 30 and you see the half-hour .
Within the event tick of the timer calls the Add method of the ComboBox and I guess you want to add the system time of the PC.

Regards.


Tuesday, October 16, 2012 10:01 PM

Is there a sample ?


Wednesday, October 17, 2012 5:50 AM

Hello sdnd2000,

Is there a sample ?

sure , one simple sample ,this sample is fo 2 second , fo 30 minutes is timer1.interval = 60000 * 30 ;

      private void button2_Click(object sender, EventArgs e)        {            timer1.Interval = 2000;            timer1.Start();        }        private void timer1_Tick(object sender, EventArgs e)        {            comboBox1.Items.Add(DateTime.Now.ToShortTimeString());        }        private void button3_Click(object sender, EventArgs e)        {            timer1.Stop();        }

Regards.


Wednesday, October 17, 2012 7:10 AM

Are you always going to start at 8am and end at 12:30pm? Or do you want to populate a number of "half hours" into the combobox based on current time?

I think Sameer's solution seens like the most correct way to go. You probably need to add some parameters for starting and ending times.

Roar Jørstad aka sveroa
Senior Consultant, EVRY as
Blog: Notebook, trick & tips
Please mark as answer or helpful if my post is useful


Wednesday, October 17, 2012 3:51 PM

It is not a sequential ?


Thursday, October 18, 2012 4:43 AM

In terms of time, following sounds sequential to me:

12:00 AM
12:30 AM
1:00 AM
1:30 AM

...

11:30 AM
12:00 PM
12:30 PM
1:00 PM

...

11:00 PM
11:30 PM

what do you expect as sequential?

If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.


Thursday, October 18, 2012 5:12 AM

Okay, on a second thought I understood what you are looking for, did you mean showing the results in following fashion:

1:00 AM
1:00 PM
1:30 AM
1:30 PM
...

11:30 AM
11:30 PM
12:00 AM
12:00 PM
12:30 AM
12:30 PM

please confirm, and I will tell you what to do.

Sameer

If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.


Thursday, October 18, 2012 6:45 AM

Hello sdnd2000,

It is not a sequential ?

my example, though simple and sequential because every 30 minutes added to the time control combobox updated by the system, possibly further explains what you need.

Regards.


Thursday, October 18, 2012 12:59 PM | 1 vote

Looks like we work in different timezone... So, instead of waiting on eachother for confirmation/replies, assuming that your expectation about sequence is what I have posted in my earlier reply, here's a modified version of GetTimeIntervals() method which is also little improved to avoid hardcoding of iterations, etc.

        public static List<string> GetTimeIntervals()        {            int intervalInMinutes = 30;     // You may want to read this from config...            List<string> timeIntervals = new List<string>();            // Since we want to start with 1 AM, 1 PM and so on...            TimeSpan startTimeAM = new TimeSpan(0, 60, 0);      // minutes corresponding to 1 AM            TimeSpan startTimePM = new TimeSpan(0, 780, 0);     // minutes corresponding to 1 PM            DateTime startDate = new DateTime(DateTime.MinValue.Ticks); // Date to be used to get shortTime format.            int iterations = (12 /* 12 hours */ * 60 /* Minutes */) / intervalInMinutes;            for (int i = 0; i < iterations; i++)            {                int minutesToBeAdded = intervalInMinutes * i;      // Increase minutes by specified interval                TimeSpan timeToBeAdded = new TimeSpan(0, minutesToBeAdded, 0);                TimeSpan tsAM = startTimeAM.Add(timeToBeAdded);                TimeSpan tsPM = startTimePM.Add(timeToBeAdded);                DateTime resultAM = startDate + tsAM;                DateTime resultPM = startDate + tsPM;                // Since we are starting with 1 hour ahead i.e. startTimeAM as 60 minutes,                 // the case of 12 AM/PM and onwards till 1 AM / PM needs to be handled separately                if (i < iterations - (60 / intervalInMinutes))                {                    timeIntervals.Add(resultAM.ToShortTimeString());                    timeIntervals.Add(resultPM.ToShortTimeString());                }                else                {                    // Reverse the order of values to be appeneded to the list to handle case of 12 AM/PM till 1 AM/PM                    timeIntervals.Add(resultPM.ToShortTimeString());                    timeIntervals.Add(resultAM.ToShortTimeString());                }            }            return timeIntervals;        }

HTH
Sameer

If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.


Tuesday, November 19, 2013 11:00 PM | 1 vote

Here is a version using only DateTime... much easier to follow and modify I think:

public List<string> GetTimeIntervals()
{
    List<string> timeIntervals = new List<string>();
    DateTime date = DateTime.MinValue.AddHours(6); // start at 6am
    DateTime endDate = DateTime.MinValue.AddDays(1).AddHours(6); // end at 5:45am (< 6am)

    while (date < endDate)
    {
        timeIntervals.Add(date.ToShortTimeString());
        date = date.AddMinutes(15);
    }

    return timeIntervals;
}

Tuesday, November 19, 2013 11:05 PM

You answered to a 13 Months old Thread.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.