3.0.0 • Published 4 years ago

ngx-mat-datetime-picker v3.0.0

Weekly downloads
768
License
MIT
Repository
github
Last release
4 years ago

Ngx Material DatetimePicker, Timepicker for @angular/material 7.x, 8.x, 9.x

Build Status codecov License npm version

Description

A DatetimePicker like @angular/material Datepicker by adding support for choosing time.

button

DEMO

@see LIVE DEMO

Alt Text

Getting started

Angular 7.x, 8.x: Install the version v2.x

npm install --save ngx-mat-datetime-picker@2.x

@see DEMO stackblitz for Angular 7, Angular 8

Angular 9.x: Install the version v3.x

npm install --save ngx-mat-datetime-picker@3.x

@see DEMO stackblitz for Angular 9

Setup

Basically the same way the @angular/material Datepicker is configured and imported.

import { NgxMatDatetimePickerModule, NgxMatTimepickerModule } from 'ngx-mat-datetime-picker';
@NgModule({
   ...
   imports: [
      BrowserModule,
      HttpClientModule,
      BrowserAnimationsModule,
      MatDatepickerModule,
      MatInputModule,
      NgxMatTimepickerModule,
      FormsModule,
      ReactiveFormsModule,
      MatButtonModule,
      NgxMatDatetimePickerModule,
   ],
   ...
})
export class AppModule { }

@see src/app/app.module.ts

Using the component

The same API as @angular/material Datepicker (@see API docs)

Datetime Picker (ngx-mat-datetime-picker)

<mat-form-field>
   <input matInput [ngxMatDatetimePicker]="picker" placeholder="Choose a date" [formControl]="dateControl"
      [min]="minDate" [max]="maxDate" [disabled]="disabled">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <ngx-mat-datetime-picker #picker [showSpinners]="showSpinners" [showSeconds]="showSeconds"
      [stepHour]="stepHour" [stepMinute]="stepMinute" [stepSecond]="stepSecond">
   </ngx-mat-datetime-picker>
</mat-form-field>

Timepicker (ngx-mat-timepicker)

<ngx-mat-timepicker [(ngModel)]="date"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [disabled]="disabled"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [stepHour]="2" [stepMinute]="5" [stepSecond]="10"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [showSpinners]="showSpinners"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [showSeconds]="showSeconds"></ngx-mat-timepicker>
<ngx-mat-timepicker [formControl]="formControl"></ngx-mat-timepicker>

List of @Input

@InputTypeDefault valueDescription
disabledbooleannullIf true, the picker is readonly and can't be modified
showSpinnersbooleantrueIf true, the spinners above and below input are visible
showSecondsbooleantrueIf true, it is not possible to select seconds
stepHournumber1The number of hours to add/substract when clicking hour spinners
stepMinutenumber1The number of minutes to add/substract when clicking minute spinners
stepSecondnumber1The number of seconds to add/substract when clicking second spinners
colorThemePaletteundefinedColor palette to use on the datepicker's calendar.
enableMeridianbooleanfalseWhether to display 12H or 24H mode.
touchUibooleanfalseWhether the calendar UI is in touch mode. In touch mode the calendar opens in a dialog rather than a popup and elements have more padding to allow for bigger touch targets.

Choosing a date implementation and date format settings

The datepicker was built to be date implementation agnostic. This means that it can be made to work with a variety of different date implementations. However it also means that developers need to make sure to provide the appropriate pieces for the datepicker to work with their chosen implementation.

The easiest way to ensure this is to import one of the provided date modules:

NgxMatNativeDateModuleNgxMatMomentModule
Date typeDateMoment
Supported localesen-USSee project for details
DependenciesNoneMoment.js
Import fromngx-mat-datetime-pickerngx-mat-moment-adapter

To use NgxMatMomentModule:

npm install --save ngx-mat-moment-adapter

Please note: NgxMatNativeDateModule is based off the functionality available in JavaScript's native Date object. Thus it is not suitable for many locales. One of the biggest shortcomings of the native Date object is the inability to set the parse format.

We highly recommend using the NgxMatMomentModule or a custom NgxMatDateAdapter that works with the formatting/parsing library of your choice.

For example:

Creating a custom date adapter:

@Injectable()
export class CustomDateAdapter extends NgxMatDateAdapter<D> {...}
// D can be Date, Moment or customized type

Creating a custom date adapter module

@NgModule({
  providers: [
    {
      provide: NgxMatDateAdapter,
      useClass: CustomDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    }
  ],
})
export class CustomDateModule { }

Theming

<link href="https://fonts.googleapis.com/icon?family=Material+Icons&display=block" rel="stylesheet">

License

MIT