19.3.2 • Published 4 months ago

@tremonsoft/datetime-picker v19.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Angular Material DatetimePicker, Timepicker for @angular/material 7.x, 8.x, 9.x, 10.x, 11.x, 12.x, 13.x, 14.x, 15.x, 16.x, 18.x, 19.x

Build Status License npm version Github All Releases

Description

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

DEMO

@see LIVE DEMO AND DOCUMENTATION

@see DEMO stackblitz

Alt Text

Choose the version corresponding to your Angular version:

Angular@ngxmc/datetime-picker
1919.x+
1818.x+
1616.x+
1515.x+ OR 9.x+ for legacy import
148.x+
137.x+
126.x+
115.x+
104.x+
92.x+
82.x+
72.x+

Getting started

npm install --save  @ngxmc/datetime-picker

Setup

Add the date provider to your app configuration.

!IMPORTANT to prevent the _ERROR Error: NgxMatDatetimePicker: No provider found for DateAdapter. You must import one of the following providers at your application root or standalone component: provideNativeDateAdapter, provideMomentDateAdapter, provideLuxonDateAdapter, provideDateFnsAdapter, or provide a custom implementation.

import { provideNativeDateAdapter } from '@angular/material/core';

export const appConfig: ApplicationConfig = {
  providers: [
    ...,
    provideNativeDateAdapter(),
    ...,
  ],
};

On your component, you can use the datepicker as follows:

import {
  NgxMatDatepickerActions,
  NgxMatDatepickerApply,
  NgxMatDatepickerCancel,
  NgxMatDatepickerClear,
  NgxMatDatepickerInput,
  NgxMatDatetimepicker,
} from '@ngxmc/datetime-picker';

@Component({
  selector: 'test',
  imports: [
    NgxMatDatepickerActions,
    NgxMatDatepickerActions,
    NgxMatDatepickerApply,
    NgxMatDatepickerCancel,
    NgxMatDatepickerClear,
    NgxMatDatepickerInput,
    NgxMatDatetimepicker,
    ..., // other imports
  ],
  template: `
    <input matInput [ngxMatDatetimePicker]="event" class="hidden" />

    <ngx-mat-datetime-picker #event>
      <ngx-mat-datepicker-actions>
        <div class="flex w-full justify-between">
          <button mat-button ngxMatDatepickerClear>Clear</button>
          <div>
            <button mat-button ngxMatDatepickerCancel>Cancel</button>
            <button mat-raised-button color="primary" ngxMatDatepickerApply>Apply</button>
          </div>
        </div>
      </ngx-mat-datepicker-actions>
    </ngx-mat-datetime-picker>
  `,
})
export class TestComponent {}

@see src/app/demo-datetime/demo-datetime.module.ts

Using the component The same API as @angular/material Datepicker (@see [API

docs](https://material.angular.io/components/datepicker/api)) ### 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"
    [touchUi]="touchUi"
    [color]="color"
    [enableMeridian]="enableMeridian"
    [disableMinute]="disableMinute"
    [hideTime]="hideTime">
  </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 [(ngModel)]="date" [disableMinute]="disableMinute"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [defaultTime]="defaultTime"></ngx-mat-timepicker>
<ngx-mat-timepicker [formControl]="formControl"></ngx-mat-timepicker>

List of @Input of ngx-mat-timepicker

You can use all @Input of ngx-mat-timepicker for ngx-mat-datetime-picker

@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
disableMinutebooleanfalseIf true, the minute (and second) is readonly
defaultTimeArrayundefinedAn array hour, minute, second for default time when the date is not yet defined
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.
hideTimebooleanfalseIf true, the time is hidden.
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

NativeDateAdapter DateFnsAdapter LuxonDateAdapter MomentDateAdapter

For example:

Creating a custom date adapter:

@Injectable()
export class CustomDateAdapter extends DateAdapter<D> {...}
// D can be Date, Moment or customized type
// If using Moment
const CUSTOM_DATE_FORMATS: NgxMatDateFormats = {
  parse: {
    dateInput: "l, LTS"
  },
  display: {
    dateInput: "l, LTS",
    monthYearLabel: "MMM YYYY",
    dateA11yLabel: "LL",
    monthYearA11yLabel: "MMMM YYYY"
  }
};

Creating a custom date adapter module

export function provideNgxMatCustomDate() {
  return makeEnvironmentProviders([
    { provide: DateAdapter, useClass: CustomDateAdapter, deps: [MAT_DATE_LOCALE] },
    { provide: NGX_MAT_DATE_FORMATS, useValue: CUSTOM_DATE_FORMATS },
  ]);
}

You can also customize the date format by providing your custom NGX_MAT_DATE_FORMATS in your module.

Theming

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

License

MIT