Click here to Skip to main content
15,877,859 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am seeking a function to populate the dropdown list with time interval. For example, when user clicks the dropdown list, there should be like:

VB
8:00AM

8:30AM

9:00AM

....

12:00PM

12:30PM


How can I do this in C#?
Posted
Updated 4-Jun-21 1:24am
v2
Comments
Hammad 15-Jan-14 2:46am    
Can't understand your qestion buddy, can you further explain it
Member 10276220 15-Jan-14 2:51am    
i have a two textbox...one is for start time & 2nd is for end time...after give these two time,i wanna populate a dropdown list with these start time & end time with 15 or 30min time interval...for eg:,it should be like
8:00am
8:15am
8:30am
.....
12:15pm
12:30pm
What have you tried and where is the problem?
Member 10276220 15-Jan-14 6:34am    
string st = ddlStartTime.SelectedItem.Text;
var s = st.Split(' ');
string ed = ddlEndTime.SelectedItem.Text;
var en = ed.Split(' ');
DateTime start = DateTime.ParseExact(s[0], "HH:mm", null);
DateTime end = DateTime.ParseExact(en[0], "HH:mm", null);
int interval = Convert.ToInt32(txtMeetingIncrements.Text);
List<string> lstTimeIntervals = new List<string>();
for (DateTime i = start; i < end; i = i.AddMinutes(interval))
lstTimeIntervals.Add(i.ToString("HH:mm tt"));

ddlAutoFilledTimes.DataSource = lstTimeIntervals;
ddlAutoFilledTimes.DataBind();
Member 10276220 15-Jan-14 6:35am    
this is my code...but dropdown isnt binding

Try this:

C#
private void populateDropdown(ComboBox dropdown, DateTime startTime, DateTime endTime, TimeSpan interval)
{
    dropdown.Items.Clear();

    DateTime time = startTime;

    while (time <= endTime)
    {
        dropdown.Items.Add(time.ToString("HH:mm tt"));
        time = time.Add(interval);
    }
}


Then call it like this:

populateDropdown(comboBox1, new DateTime(2000, 1, 1, 8, 0, 0), new DateTime(2000, 1, 1, 12, 30, 0), new TimeSpan(0, 30, 0));
 
Share this answer
 
On top of above two solutions,
you can try this too..

C#
DateTime start = DateTime.ParseExact(txtStart.Text,"HH:mm tt", null);
       DateTime end = DateTime.ParseExact(txtEnd.Text, "HH:mm tt", null);

       int interval = 30;
       List<string> lstTimeIntervals = new List<string>();
       for (DateTime i = start; i < end; i =i.AddMinutes(interval))
           lstTimeIntervals.Add(i.ToString("HH:mm tt"));

       dropdownList.DataSource = lstTimeIntervals;
       dropdownList.DataBind();
 
Share this answer
 
Comments
Member 10276220 15-Jan-14 6:33am    
its not working...gettnf error of convertion
Member 10276220 15-Jan-14 6:35am    
this is my code...

string st = ddlStartTime.SelectedItem.Text;
var s = st.Split(' ');
string ed = ddlEndTime.SelectedItem.Text;
var en = ed.Split(' ');
DateTime start = DateTime.ParseExact(s[0], "HH:mm", null);
DateTime end = DateTime.ParseExact(en[0], "HH:mm", null);
int interval = Convert.ToInt32(txtMeetingIncrements.Text);
List lstTimeIntervals = new List();
for (DateTime i = start; i < end; i = i.AddMinutes(interval))
lstTimeIntervals.Add(i.ToString("HH:mm tt"));

ddlAutoFilledTimes.DataSource = lstTimeIntervals;
ddlAutoFilledTimes.DataBind();

but dropdown isnt binding
Karthik_Mahalingam 15-Jan-14 6:40am    
what is the text on st ??
Member 10276220 15-Jan-14 7:07am    
st will get "10:00 am"
Karthik_Mahalingam 15-Jan-14 7:15am    
replce "HH:mm" with "hh:mm" in my solution...
C#
public List<string> GetTimeIntervals()
{
	List<string> timeIntervals = new List<string>();
	DateTime date = DateTime.MinValue.AddHours(6); 
	DateTime endDate = DateTime.MinValue.AddDays(1).AddHours(6); 
	while (date < endDate)
	{
		timeIntervals.Add(date.ToShortTimeString());
		date = date.AddMinutes(15);
	}
	return timeIntervals;
}</string>
 
Share this answer
 
v3
SQL
JUST EXECUTE THIS SP IN SQL AND BIND YOUR dropdown list



ALTER PROC TIMELIST(@Stime NVARCHAR(20),@Etime NVARCHAR(20),@MINInterval INT )
AS
BEGIN

DECLARE @newtime NVARCHAR(20)
SET @newtime=@Stime
DECLARE @SRnO INT
SET @SRnO=1

DECLARE @TEM TABLE (ID INT ,TIMES NVARCHAR(20))

WHILE(CONVERT(TIME,@newtime)<=CONVERT(TIME,@Etime))
BEGIN
   INSERT INTO @TEM VALUES(@SRnO,CONVERT(varchar(15),CAST(@newtime AS TIME),100))
    SET @SRnO=@SRnO+1
	SET @newtime=dateadd(MINUTE, @MINInterval,CONVERT(TIME,@newtime) )
END

SELECT * FROM @TEM
END

--EXEC  TIMELIST '8:00AM','12:30PM',30 


ID	TIMES
1	8:00AM
2	8:30AM
3	9:00AM
4	9:30AM
5	10:00AM
6	10:30AM
7	11:00AM
8	11:30AM
9	12:00PM
10	12:30PM
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900