Answered by:
Populate dropdown list with time range selection with 15 minute interval + mvc + c#

Question
-
User1878001738 posted
Hi,
I am doing an appointment system web project and what I need to do is to have a dropdown list with selection of time range with interval.
For example,
Start Time: 7:00 AM
End Time: 9:00 AM
Interval: 15 minutes
What I must show in dropdown list is like this..
7:00 - 7:15 7:15 - 7:30 7:30 - 7:45 7:45 - 8:00 8:00 - 8:15 8:15 - 8:30 8:30 - 8:45 8:45 - 9:00 Can someone help me through on how to do this?
Thanks in advance.
Tuesday, December 17, 2019 6:17 AM
Answers
-
User1878001738 posted
Hi,
Already found the solution to this problem. This is what I did.
var startTime = DateTime.Parse("7:00"); var endTime = DateTime.Parse("9:00"); List<string> time_list = new List<string>(); while (startTime < endTime) { time_list.Add(startTime.ToShortTimeString() + " - " + startTime.AddMinutes(15).ToShortTimeString()); startTime = startTime.AddMinutes(15); } foreach (var a in time_list) { System.Console.WriteLine(a); }
Thank you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2019 8:44 AM -
User-719153870 posted
Hi loraine26,
You can use multiple ways to bind data for dropdownlist in Asp.net mvc dynamically. Please check Different Ways Of Binding Razor DropdownList In ASP.NET MVC 5.
I think the confusing part for you is how to add 15 minutes for the time, you can check DateTime.AddMinutes method.
Also, please check below demo using the ViewBag to bind the data:
cshtml:
<html> <head> <meta name="viewport" content="width=device-width" /> <title>DDLDemo</title> </head> <body> <div> @Html.DropDownList("DateDDL", ViewBag.DDLList as IEnumerable<SelectListItem>) </div> </body> </html>
controller:
public ActionResult DDLDemo() { DateTime start = new DateTime(2019, 12, 17, 7, 0, 0); DateTime end = new DateTime(2019, 12, 17, 9, 0, 0); int i = 0; List<SelectListItem> list = new List<SelectListItem>(); while (start.AddMinutes(15) <= end) { list.Add(new SelectListItem() { Text= start.ToString("t")+" - "+start.AddMinutes(15).ToString("t"), Value=i.ToString()});
start = start.AddMinutes(15);
i += 1; } ViewBag.DDLList = list; return View(); }Below is the result of this demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2019 8:52 AM
All replies
-
User1878001738 posted
Hi,
Already found the solution to this problem. This is what I did.
var startTime = DateTime.Parse("7:00"); var endTime = DateTime.Parse("9:00"); List<string> time_list = new List<string>(); while (startTime < endTime) { time_list.Add(startTime.ToShortTimeString() + " - " + startTime.AddMinutes(15).ToShortTimeString()); startTime = startTime.AddMinutes(15); } foreach (var a in time_list) { System.Console.WriteLine(a); }
Thank you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2019 8:44 AM -
User-719153870 posted
Hi loraine26,
You can use multiple ways to bind data for dropdownlist in Asp.net mvc dynamically. Please check Different Ways Of Binding Razor DropdownList In ASP.NET MVC 5.
I think the confusing part for you is how to add 15 minutes for the time, you can check DateTime.AddMinutes method.
Also, please check below demo using the ViewBag to bind the data:
cshtml:
<html> <head> <meta name="viewport" content="width=device-width" /> <title>DDLDemo</title> </head> <body> <div> @Html.DropDownList("DateDDL", ViewBag.DDLList as IEnumerable<SelectListItem>) </div> </body> </html>
controller:
public ActionResult DDLDemo() { DateTime start = new DateTime(2019, 12, 17, 7, 0, 0); DateTime end = new DateTime(2019, 12, 17, 9, 0, 0); int i = 0; List<SelectListItem> list = new List<SelectListItem>(); while (start.AddMinutes(15) <= end) { list.Add(new SelectListItem() { Text= start.ToString("t")+" - "+start.AddMinutes(15).ToString("t"), Value=i.ToString()});
start = start.AddMinutes(15);
i += 1; } ViewBag.DDLList = list; return View(); }Below is the result of this demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2019 8:52 AM