Example of Time Range in ASP.NET MVC TimePicker Control
Select a start time from the first TimePicker and then the second TimePicker will be enabled. Select an end time from the second TimePicker to get a time range. Click/Touch the Business Hours checkbox to change both the TimePickers to read-only state.
@Html.UiTimePicker("start", new UiTimePickerOptions { Placeholder = "Start Time", OnChange = "onEnableEndTime" })
@Html.UiTimePicker("end", new UiTimePickerOptions { Placeholder = "End Time", Enabled = false })
@Html.UiCheckBox("dayRange", false, new UiCheckBoxOptions { Id = "dayRange", Label = "Business Hours", OnChange = "changeTime" })
<style>
.control_wrapper { max-width: 400px; margin: 0 auto; padding: 2%; }
.tabs-wrap { padding: 10px; }
</style>
<div class="control-section">
<div class="control_wrapper">
<div class="pane">
<div class="tabs-wrap">
<div class="wrap">
@Html.UiTimePicker("start", new UiTimePickerOptions { Placeholder = "Start Time", OnChange = "onEnableEndTime" })
</div>
</div>
<div class="tabs-wrap">
<div class="wrap">
@Html.UiTimePicker("end", new UiTimePickerOptions { Placeholder = "End Time", Enabled = false })
</div>
</div>
<div class="tabs-wrap" style="padding: 14px 10px;">
<div class="wrap">
@Html.UiCheckBox("dayRange", false, new UiCheckBoxOptions { Id = "dayRange", Label = "Business Hours", OnChange = "changeTime" })
</div>
</div>
</div>
</div>
</div>
<script>
var step = 30; // the default Step of the TimePicker, in minutes
// Enables the end time once a start time is selected; it can't be before the start time (plus a step).
function onEnableEndTime(value) {
var endTime = window.Lumen.get('end');
endTime.enable();
endTime.setValue(null);
if (value) {
var min = new Date(+value);
min.setMinutes(min.getMinutes() + step);
endTime.setMin(min);
}
}
// Business Hours: 9:00 to 18:00, and both pickers are read-only.
function changeTime(checked) {
var startTime = window.Lumen.get('start');
var endTime = window.Lumen.get('end');
if (checked) {
startTime.setValue(new Date(2017, 8, 6, 9, 0));
endTime.enable();
endTime.setMin(null);
endTime.setValue(new Date(2017, 8, 6, 18, 0));
startTime.setReadOnly(true);
endTime.setReadOnly(true);
} else {
endTime.setValue(null);
startTime.setValue(null);
startTime.setReadOnly(false);
endTime.setReadOnly(false);
endTime.disable();
}
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TimePickerController : Controller
{
// The page is Views/TimePicker/TimeRange.cshtml; it draws what ViewData holds.
public ActionResult TimeRange()
{
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
Time Range sample illustrates the appointment time selection scenario with the start and end time option. Here, two TimePicker components are used to select the start and end time.
Before the start time selection, the end time TimePicker is in disable state. When the start time is selected, then you will be able to select the end time or else, need to select the entire business hours 9:00 to 18:00 from the Business Hours option. Once the options are checked, both the TimePicker components goes to readonly state.