NgTimeparser is a Angular 5 service that helps you parse any time from a input field to a correct time. It can handle both 12/24 hour formats.
Install from npm
npm i ng-timeparser
Include in NgModule
@NgModule({
declarations: [
...
],
imports: [
...
TimeParserModule
]
You can either use the service directly or use the directive on your input-field.
12 hour format
<input type="text" [(ngModel)]="inputTime" timeParser />
24 hour format
<input type="text" [(ngModel)]="inputTime" timeParser militaryTime='true' />
Include service to component
constructor(private timeParserService: TimeParserService) {}
12 hour format
this.time = this.timeParserService.parseTime(this.time, false);
24 hour format
this.time = this.timeParserService.parseTime(this.time, true);
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
Run ng test
to execute the unit tests via Karma.