Time masking in EJ2 TypeScript TimePicker
4 Sep 20269 minutes to read
The TimePicker has the enableMask property that provides the option to enable the built-in date masking support. The MaskedDateTime module must also be injected to enable the masking support.
import { TimePicker, MaskedDateTime } from '@syncfusion/ej2-calendars';
TimePicker.Inject(MaskedDateTime);
let timepickerObject: TimePicker = new TimePicker({
enableMask: true
});
timepickerObject.appendTo('#mask');<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 DatePicker 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" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-calendars/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<input id='mask' />
</div>
</body>
</html>The mask pattern is defined based on the provided time format to the component. If the format is not specified, the mask pattern is formed based on the default format of the current culture.
| Keys | Actions |
|---|---|
| Up / Down arrows | To increment and decrement the selected portion of the time. |
| Left / Right arrows and Tab | To navigate the selection from one portion to the next portion |
The following example demonstrates default and custom format of the TimePicker component with mask.
import { TimePicker, MaskedDateTime } from '@syncfusion/ej2-calendars';
TimePicker.Inject(MaskedDateTime);
let timepickerObject: TimePicker = new TimePicker({
enableMask: true
});
timepickerObject.appendTo('#mask');
let timepickerFormat: TimePicker = new TimePicker({
enableMask: true,
format: "h:mm a",
});
timepickerFormat.appendTo('#format');<!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" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-calendars/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:50px auto 0; width:250px;">
<br><br>
<label class="custom-input-label">Mask support with default format</label>
<br><br>
<input id='mask' />
<br><br><br>
<label class="custom-input-label">Mask support with custom format</label>
<br><br>
<input id='format' />
</div>
</div>
</body>
</html>Configure Mask Placeholder
The mask placeholder value can be changed through the maskPlaceholder property. By default, it takes the full name of time co-ordinates such as hour, minute and second.
While changing to a culture other than English, ensure that locale text for the concerned culture is loaded through the load method of the L10n class for mask placeholder values like below.
//Load the L10n from ej2-base
import { L10n } from '@syncfusion/ej2-base';
//load the locale object to set the localized mask placeholder value
L10n.load({
'de': {
'timepicker': { hour: 'Stunde' ,minute: 'Minute', second:'Sekunde' }
}
});The following example demonstrates default and customized mask placeholder value.
import { TimePicker, MaskedDateTime } from '@syncfusion/ej2-calendars';
TimePicker.Inject(MaskedDateTime);
let timepickerObject: TimePicker = new TimePicker({
enableMask: true
});
timepickerObject.appendTo('#mask');
let timepickerPlaceholder: TimePicker = new TimePicker({
enableMask: true,
maskPlaceholder: {hour: 'h', minute: 'm', second: 's'}
});
timepickerPlaceholder.appendTo('#placeholder');<!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" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-calendars/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:50px auto 0; width:250px;">
<br><br>
<label class="custom-input-label">Default mask placeholder</label>
<br><br>
<input id='mask' />
<br><br><br>
<label class="custom-input-label">Custom mask placeholder</label>
<br><br>
<input id='placeholder' />
</div>
</div>
</body>
</html>