Time masking in Angular Timepicker component
27 Apr 20245 minutes to read
TimePicker has enableMask
property that provides the option to enable the built-in time masking support. Also, you must inject the MaskedDateTime module to enable the masking support.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TimePickerModule } from '@syncfusion/ej2-angular-calendars';
import { Component } from '@angular/core';
import { MaskedDateTimeService } from '@syncfusion/ej2-angular-calendars';
@Component({
imports: [
TimePickerModule
],
standalone: true,
selector: 'app-root',
template: `
<ejs-timepicker [enableMask]="enableMaskSupport"></ejs-timepicker>
`,
providers: [MaskedDateTimeService],
})
export class AppComponent {
public format: string = "HH:mm";
public enableMaskSupport: boolean = true;
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
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 next portion |
The following example demonstrates default and custom format of TimePicker component with mask.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TimePickerModule } from '@syncfusion/ej2-angular-calendars';
import { Component } from '@angular/core';
import { MaskedDateTimeService } from '@syncfusion/ej2-angular-calendars';
@Component({
imports: [
TimePickerModule
],
standalone: true,
selector: 'app-root',
templateUrl: './format.html',
providers: [MaskedDateTimeService],
})
export class AppComponent {
constructor( ) {
}
public format: string = "hh:mm:ss";
public enableMaskSupport: boolean = true;
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Configure Mask Placeholder
You can change mask placeholder value through property maskPlaceholder
. 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 load method of 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 { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TimePickerModule } from '@syncfusion/ej2-angular-calendars';
import { Component } from '@angular/core';
import { MaskedDateTimeService } from '@syncfusion/ej2-angular-calendars';
@Component({
imports: [
TimePickerModule
],
standalone: true,
selector: 'app-root',
templateUrl: './maskplaceholder.html',
providers: [MaskedDateTimeService],
})
export class AppComponent {
constructor( ) {
}
public enableMaskSupport: boolean = true;
public maskPlaceholderValue: Object = {hour: 'h', minute: 'm', second: 's'}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));