Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap TimePicker

Ngx Bootstrap TimePicker

Ngx Bootstrap TimePicker

A Time Picker component that is easy to use and highly configurable is provided by the ngx-bootstrap timepicker component.

TimepickerComponent

selector

  • timepicker

Inputs

  • arrowkeys −Boolean arrow keys, if true, use the up/down arrow keys on the keyboard to change the values of hours and minutes.
  • disabled-Boolean if fields of actual hours and minutes are disabled.
  • hoursPlaceholder − string, placeholder in timepicker for hours sector.
  • hourStep-number, phase shift hours.
  • max − Date, the user can pick the maximum time.
  • meridians − string[], meridian labels dependent on locale.
  • min − Date, minimum time user can pick.
  • minutesPlaceholder − string, placeholder in timepicker for minutes area.
  • minuteStep − number, phase change hours.
  • mousewheel − Boolean mousewheel, if actual inputs scroll within hours and minutes, time will change.
  • readonlyInput-Boolean, if actual fields for hours and minutes are readonly.
  • secondsPlaceholder − string, placeholder in the timepicker field for seconds.
  • secondsStep − number, step changes in seconds.
  • showMeridian − boolean, if real meridian button will be shown.
  • showMinutes − ShowMinutes, Boolean, Timepicker show minutes.
  • showSeconds −ShowSeconds, Boolean, Timepicker show seconds.
  • showSpinners − ShowSpinners-boolean, if the inputs are shown with true spinner arrows above and below.

Outputs

  • isValid − emits true if value is a valid date.

Example

We need to update the app.module.ts used in the ngx-bootstrap Tabs chapter to use TimepickerModule as we're going to use TimePicker.

In order to use the TimepickerModule, update app.module.ts.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { AccordionModule } from 'ngx-bootstrap/accordion';
import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert';
import { ButtonsModule } from 'ngx-bootstrap/buttons';
import { FormsModule } from '@angular/forms';
import { CarouselModule } from 'ngx-bootstrap/carousel';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { BsDatepickerModule, BsDatepickerConfig } from 'ngx-bootstrap/datepicker';
import { BsDropdownModule,BsDropdownConfig } from 'ngx-bootstrap/dropdown';
import { PaginationModule,PaginationConfig } from 'ngx-bootstrap/pagination';
import { PopoverModule, PopoverConfig } from 'ngx-bootstrap/popover';
import { ProgressbarModule,ProgressbarConfig } from 'ngx-bootstrap/progressbar';
import { RatingModule, RatingConfig } from 'ngx-bootstrap/rating';
import { SortableModule, DraggableItemService } from 'ngx-bootstrap/sortable';
import { TabsModule, TabsetConfig } from 'ngx-bootstrap/tabs';
import { TimepickerModule } from 'ngx-bootstrap/timepicker';

@NgModule({
   declarations: [
      AppComponent,
      TestComponent
   ],
   imports: [
      BrowserAnimationsModule,
      BrowserModule,
      AccordionModule,
      AlertModule,
      ButtonsModule,
      FormsModule,
      CarouselModule,
      CollapseModule,
      BsDatepickerModule.forRoot(),
      BsDropdownModule,
      ModalModule,
      PaginationModule,
      PopoverModule,
      ProgressbarModule,
      RatingModule,
      SortableModule,
      TabsModule,
      TimepickerModule.forRoot()
   ],
   providers: [AlertConfig, 
      BsDatepickerConfig, 
      BsDropdownConfig,
      BsModalService,
      PaginationConfig,
      ProgressbarConfig,
      RatingConfig,
      DraggableItemService,
      TabsetConfig],
   bootstrap: [AppComponent]
})
export class AppModule { }

Update test.component.html to use the timepicker component.

test.component.html

<timepicker [(ngModel)]="time"></timepicker>
<pre class="alert alert-info">Time is: {{time}}</pre>

<timepicker [(ngModel)]="time" [showMeridian]="false"></timepicker>
<pre class="alert alert-info">Time is: {{time}}</pre>

Update test.component.ts for corresponding variables and methods.

test.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
   selector: 'app-test',
   templateUrl: './test.component.html',
   styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
   time: Date = new Date();
   constructor() {}
   ngOnInit(): void {
   } 
}

Build and Serve

Run the following command to start the angular server.

ng serve

No Sidebar ads