Time Range

21 Dec 20228 minutes to read

TimePicker provides an option to select a time value within a specified range by using the min and max properties. The min value should always be lesser than the max value.

When the min and max properties are configured and the selected time value is out-of-range or invalid, then the model value will be set to out of range time value or null respectively with highlighted error class to indicates the time is out of range or invalid.

The value property depends on the min/max with respect to strictMode property.
The following example allows you to select a time value within a range of 9:00 AM to 11:30 AM.

@Html.EJS().TimePicker("timepicker").Value(ViewBag.value).Min(ViewBag.minVal).Max(ViewBag.maxVal).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public class HomeController: Controller
    {
        public ActionResult DefaultFunctionalities()
        {
            ViewBag.minVal= new DateTime(2017,08,03, 09,00,00);
            ViewBag.maxVal = new DateTime(2017,08,03, 11,30,00);
            ViewBag.value =  new DateTime(2017,08,03, 11,00,00);
            return View();
        }
    }
}

NOTE

If the value of min or max property is changed through code behind you have to update the value property to set within the range.

Time Range customization using two TimePicker components

Here, two TimePicker components are used to select the start and end time. The below sample illustrates the appointment time selection scenario with the start and end time option.

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.

<div class="control-section">
        <div class="control_wrapper">
            <div class="pane">
                <div class="tabs-wrap">
                    <div class="wrap">
                        @Html.EJS().TimePicker("start").Created("onStartCreate").Placeholder("Start Time").Change("onEnableEndTime").Render()
                    </div>
                </div>
                <div class="tabs-wrap" style="clear: both">
                    <div class="wrap">
                        @Html.EJS().TimePicker("end").Created("onEndCreate").Placeholder("End Time").Enabled(false).Render()
                    </div>
                </div>
                <div class="tabs-wrap" style="clear: both;padding: 14px 10px;">
                    <div class="wrap">
                        @Html.EJS().CheckBox("dayRange").Change("changeTime").Label("Business Hours").Render()
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
    document.addEventListener('DOMContentLoaded', function () {
        isStartTimeChange = true;
        endInput = document.getElementById('end');
    });
    function onEnableEndTime(args) {
        /*Enables end time if start time is selected*/
        if (isStartTimeChange) {
            endTime.enabled = true;
            endTime.value = null;
            endInput.value = '';
            value = new Date(+args.value);
            value.setMinutes(value.getMinutes() + endTime.step);
            endTime.min = value;
        } else {
            isStartTimeChange = true;
        }
    }

    function changeTime() {
        /*To determine whether we have selected business hours or not*/
        let element = document.getElementById('dayRange');
        isStartTimeChange = false;
        if (element.checked) {
            /*Business hours*/
            startTime.value = new Date('9/6/2017 9:00');
            endTime.enabled = true;
            endTime.value = new Date('9/6/2017 18:00');
            startTime.readonly = true;
            endTime.readonly = true;
        } else {
            endTime.value = null;
            startTime.value = null;
            endInput.value = '';
            startTime.readonly = false;
            endTime.readonly = false;
            endTime.enabled = false;
        }
    }

    function onStartCreate() {
        startTime = document.getElementById('start').ej2_instances[0];
    }
    function onEndCreate() {
        endTime = document.getElementById('end').ej2_instances[0];
    }


</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public class HomeController: Controller
    {
        public ActionResult DefaultFunctionalities()
        {
            return View();
        }
    }
}