Time range in EJ2 JavaScript Timepicker control

26 Apr 202310 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.

ej.base.enableRipple(true);
   
    var timepicker = new ej.calendars.TimePicker({
        placeholder: 'Select a time',
          //sets the min value
    min: new Date('3/8/2017 9:00 AM'),
    //sets the max value
    max: new Date('3/8/2017 11:30 AM'),
    //sets the value
    value: new Date('3/8/2017 11:00 AM')
    });    
    timepicker.appendTo('#element');
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 TimePicker control</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <!--style reference from the TimePicker component-->
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-calendars/styles/material.css" rel="stylesheet">
    <link href="style.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <input type="text" id="element">
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

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.

ej.base.enableRipple(true);
    
    var value;
    var isStartTimeChange = true;
    var startTime = new ej.calendars.TimePicker({
        placeholder: 'Start Time',
        change: onEnableEndTime
    });
    startTime.appendTo('#start');
    var endTime = new ej.calendars.TimePicker({
        placeholder: 'End Time',
        enabled: false
    });
    endTime.appendTo('#end');

    var checkboxObject = new ej.buttons.CheckBox({ label: 'Business Hours', change: changeTime });
    checkboxObject.appendTo('#dayRange');

    var endInput = document.getElementById('end');

    function changeTime(args) {
        isStartTimeChange = false;
        if (args.checked) {
            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 onEnableEndTime(args) {
        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;
        }
    }
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 TimePicker control</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <!--style reference from the TimePicker component-->
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-calendars/styles/material.css" rel="stylesheet">
    <link href="style.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="pane">
            <div class="tabs-wrap">
                <div class="wrap">
                    <input type="text" id="start">
                </div>
            </div>
            <div class="tabs-wrap" style="clear: both">
                <div class="wrap">
                    <input type="text" id="end">
                </div>
            </div>
            <div class="tabs-wrap" style="clear: both;padding: 14px 10px;">
                <div class="wrap">
                    <input type="checkbox" id="dayRange">
                </div>
            </div>
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>