16.1.0 • Published 12 months ago

@tchitos/datetime-picker v16.1.0

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

Angular DateTimePickerLibrary

npm npm

Angular dateTime picker - This package supports Angular 16+ Checkout the Demo

Installation

  1. Install package from npm.
npm install @tchitos/datetimePicker --save
  1. Include DatetimePickerModule into your application.
//...
import { DatetimePickerModule } from '@tchitos/datetime-picker';

@NgModule({
  imports: [..., DatetimePickerModule]
})
export class SomeModule {}

And that's it, you can then use it in your component as:

import { FormGroup, FormBuilder } from '@angular/forms';
import { DatePickerOptions } from '@tchitos/datetime-picker';


options: DatePickerOptions = {
  placeholder: 'Start date',
  enableHour: true
};

formGroup: FormGroup;

constructor(private fb: FormBuilder) {
    this.formGroup = this.fb.group({
        date: [new Date() ]
    })
}
<form [formGroup]="formGroup">
  <ngx-datetimePicker [options]="options" formControlName="date"> </ngx-datetimePicker>
</form>

Options

Then you apply custom options in your template as:

today = new Date();
<form [formGroup]="formGroup">
  <ngx-datetimePicker formControlName="date" [minDate]="today"> </ngx-datetimePicker>
</form>

All the properties for DatePickerOptions are optional

NameTypeDefaultDescription
minYearnumbergetYear(new Date()) - 20minimum available and selectable year
maxYearnumbergetYear(new Date()) + 20maximum available and selectable year
placeholderstring | placeholder in case date model is null or undefined, example: 'Please pick a date'
formatstringdd/MM/yyyydate format to display in input
formatTitlestringLLLL yyyydate format to change view
formatDaysstringEEEEE
firstCalendarDaynumber00 - Sunday, 1 - Monday
localeLocale0date-fns locale
positionstringbottomdate-fns locale
inputClassstringdatepicker-inputcustom input CSS class to be applied
calendarClassstringdatepicker-defaultcustom datepicker calendar CSS class to be applied
scrollBarColorstring#dfe3e9in case you customize you theme, here you define scroll bar color
enableKeyboardbooleantrueenable keyboard events
enableHourbooleanfalseenable time picker

For available format, formatTitle and formatDays options check out here.

You can also update your options in your template as:

import { DatePickerOptions } from '@tchitos/datetime-picker';
import * as localFR from 'date-fns/locale/fr';
import * as localUS from 'date-fns/locale/en-US';

  optionUpdate: BehaviorSubject<DatePickerOptions> = new BehaviorSubject<DatePickerOptions>({});

// options sample with default values
  options: DatePickerOptions = {
      format: 'dd/MM/yyyy',
      enableHour: true//enabling the time
    };


  changeToFrench() {
    this.options = {
      ...this.options,
      format: 'dd/MM/yyyy',// french date format is day/month/year
      locale: localFR.default // changing the datepicker's language to french
    }
    this.optionUpdate.next(this.options)
  }

  changeToEnglish() {
    this.options = {
      ...this.options,
      format: 'MM/dd/yyyy', // USA date format is month/day/year
      locale: localUS.default // changing the datepicker's language to english
    }
    this.optionUpdate.next(this.options)
  }

// to enable and disable

  isDisabled = false;

  toggleDisable() {
    this.formGroup.controls.date[this.isDisabled ? 'enable' : 'disable']();
    this.isDisabled = !this.isDisabled;
  }
<form [formGroup]="formGroup">
  <ngx-datetimePicker formControlName="date" [options]="options" [optionUpdate]="optionUpdate"> </ngx-datetimePicker>

  <h2>Change the formatv and langugae of the date picker</h2>

  <button (click)="changeToFrench()">French (France)</button>
  <button (click)="changeToEnglish()">English (USA)</button>

  <h2>Enable /disable</h2>

  <button (click)="toggleDisable()">{{ isDisabled ? "enable" : "disable" }}</button>
</form>

Theme customization

This examples uses SASS as style preprocessor.

.material {
  .calendar-container {
    background-color: #493c80;
    border: 1px solid #493c80;
  }
}

And in your component:

import { DatepickerOptions } from "@tchitos/datetime-picker";

options: DatepickerOptions = {
  calendarClass: "material"
};

Run Project

  1. Clone this repository.
git clone https://github.com/aminetchitooss/angular-datetime-picker
  1. Install dependencies
npm install
  1. Start the demo
npm start

License

MIT