4.2.1 • Published 4 years ago

mydaterangepicker v4.2.1

Weekly downloads
8,420
License
MIT
Repository
github
Last release
4 years ago

mydaterangepicker

New daterangepicker library

If your Angular version is >= 7.2 you can use a new version (angular-mydatepicker) of this component:

Version compatibility of this library

Library versionAngular versionBranchInstallation
< 9.0.0>= 2 and < 9masternpm install --save mydaterangepicker@latest
>= 9.0.0>= 9angularIvynpm install --save mydaterangepicker@ng9

Angular date range picker

Build Status codecov npm npm

Description

Highly configurable Angular date range picker. Compatible Angular2+.

Online demo is here

Installation

To install this component to an external project, follow the procedure:

  1. npm install mydaterangepicker --save
  2. Add MyDateRangePickerModule import to your @NgModule like example below

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { MyTestApp } from './my-test-app';
    import { MyDateRangePickerModule } from 'mydaterangepicker';
    
    @NgModule({
        imports:      [ BrowserModule, MyDateRangePickerModule ],
        declarations: [ MyTestApp ],
        bootstrap:    [ MyTestApp ]
    })
    export class MyTestAppModule {}

Usage

Use one of the following three options.

1. ngModel binding

In this option the ngModel binding is used. Here is an example application. It shows how to use the ngModel.

To use ngModel define the application class as follows:

import {IMyDrpOptions} from 'mydaterangepicker';
// other imports here...

export class MyTestApp {

    myDateRangePickerOptions: IMyDrpOptions = {
        // other options...
        dateFormat: 'dd.mm.yyyy',
    };

    // For example initialize to specific date (09.10.2018 - 19.10.2018). It is also possible
    // to set initial date range value using the selDateRange attribute.
    private model: any = {beginDate: {year: 2018, month: 10, day: 9},
                             endDate: {year: 2018, month: 10, day: 19}};

    constructor() { }
}

Add the following snippet inside your template:

<form #myForm="ngForm" novalidate>
    <my-date-range-picker name="mydaterange" [options]="myDateRangePickerOptions"
                    [(ngModel)]="model" required></my-date-range-picker>
</form>

2. Reactive forms

In this option the value accessor of reactive forms is used. Here is an example application. It shows how to use the formControlName.

To use reactive forms define the application class as follows:

import {IMyDrpOptions} from 'mydaterangepicker';
// other imports here...

export class MyTestApp implements OnInit {

    myDateRangePickerOptions: IMyDrpOptions = {
        // other options...
        dateFormat: 'dd.mm.yyyy',
    };

    private myForm: FormGroup;

    constructor(private formBuilder: FormBuilder) { }

    ngOnInit() {
        this.myForm = this.formBuilder.group({
            // Empty string means no initial value. Can be also specific date range for example:
            // {beginDate: {year: 2018, month: 10, day: 9}, endDate: {year: 2018, month: 10, day: 19}}
            // which sets this date range to initial value. It is also possible to set initial
            // value using the selDateRange attribute.

            myDateRange: ['', Validators.required]
            // other controls are here...
        });
    }

    setDateRange(): void {
        // Set date range (today) using the patchValue function
        let date = new Date();
        this.myForm.patchValue({myDateRange: {
            beginDate: {
                year: date.getFullYear(),
                month: date.getMonth() + 1,
                day: date.getDate()
            },
            endDate: {
                year: date.getFullYear(),
                month: date.getMonth() + 1,
                day: date.getDate()
            }
        }});
    }

    clearDateRange(): void {
        // Clear the date range using the patchValue function
        this.myForm.patchValue({myDateRange: ''});
    }
}

Add the following snippet inside your template:

<form [formGroup]="myForm" novalidate>
    <my-date-range-picker name="mydaterange" [options]="myDateRangePickerOptions"
                    formControlName="myDateRange"></my-date-range-picker>
  <!-- other controls are here... -->
</form>

3. Callbacks

In this option the mydaterangepicker sends data back to host application using callbacks. Here is an example application. It shows how to use callbacks.

To use callbacks define the application class as follows:

import {IMyDrpOptions, IMyDateRangeModel} from 'mydaterangepicker';
// other imports here...

export class MyTestApp {

    myDateRangePickerOptions: IMyDrpOptions = {
        // other options...
        dateFormat: 'dd.mm.yyyy',
    };

    constructor() { }

    // dateRangeChanged callback function called when the user apply the date range. This is
    // mandatory callback in this option. There are also optional inputFieldChanged and
    // calendarViewChanged callbacks.
    onDateRangeChanged(event: IMyDateRangeModel) {
        // event properties are: event.beginDate, event.endDate, event.formatted,
        // event.beginEpoc and event.endEpoc
    }
}

Add the following snippet inside your template:

<my-date-range-picker [options]="myDateRangePickerOptions"
                   (dateRangeChanged)="onDateRangeChanged($event)"></my-date-range-picker>

Attributes

options attribute

Value of the options attribute is a type of IMyDrpOptions. It can contain the following properties.

OptionDefaultTypeDescription
dayLabels{su: 'Sun', mo: 'Mon', tu: 'Tue', we: 'Wed', th: 'Thu', fr: 'Fri', sa: 'Sat'}IMyDayLabelsDay labels visible on the selector.
monthLabels{ 1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec' }IMyMonthLabelsMonth labels visible on the selector.
dateFormatyyyy-mm-ddstringDate format on the selection area and the callback. For example: dd.mm.yyyy, yyyy-mm-dd, dd mmm yyyy (mmm = Month as a text)
showClearBtntruebooleanShow the 'Clear' button on calendar.
showApplyBtntruebooleanShow the 'Apply' button on calendar.
showSelectDateTexttruebooleanShow the select date text.
selectBeginDateTxtSelect Begin DatestringSelect begin date text. Can be used if showSelectDateText = true.
selectEndDateTxtSelect End DatestringSelect end date text. Can be used if showSelectDateText = true.
firstDayOfWeekmostringFirst day of week on calendar. One of the following: mo, tu, we, th, fr, sa, su
sunHighlighttruebooleanSunday red colored on calendar.
markCurrentDaytruebooleanIs current day (today) marked on calendar.
markCurrentMonthtruebooleanIs current month marked on calendar. Can be used if monthSelector = true.
markCurrentYeartruebooleanIs current year marked on calendar. Can be used if yearSelector = true.
monthSelectortruebooleanIf month label is selected opens a selector of months.
yearSelectortruebooleanIf year label is selected opens a selector of years.
minYear1100numberMinimum allowed year in calendar. Cannot be less than 1100.
maxYear9100numberMaximum allowed year in calendar. Cannot be more than 9100.
disableUntilno default valueIMyDateDisable dates backward starting from the given date. For example: {year: 2016, month: 6, day: 26}. To reset existing disableUntil value set: {year: 0, month: 0, day: 0}
disableSinceno default valueIMyDateDisable dates forward starting from the given date. For example: {year: 2016, month: 7, day: 22}. To reset existing disableSince value set: {year: 0, month: 0, day: 0}
disableDatesno default valueArray[IMyDate](https://github.com/kekeh/mydaterangepicker/blob/master/src/my-date-range-picker/interfaces/my-date.interface.ts)Disable single dates one by one. The disabled date cannot be selected but it can be in a range. For example: {year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}. To reset existing disableDates value set empty array to it.
enableDatesno default valueArray[IMyDate](https://github.com/kekeh/mydaterangepicker/blob/master/src/my-date-range-picker/interfaces/my-date.interface.ts)Enable given dates one by one if the date is disabled. For example if you disable the date range and want to enable some dates in range. Array of enabled days. For example: {year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}. To reset existing enableDates value set empty array to it.
disableDateRangesno default valueArray[IMyDateRange](https://github.com/kekeh/mydaterangepicker/blob/master/src/my-date-range-picker/interfaces/my-date-range.interface.ts)Disable date ranges one by one. The disabled date cannot be selected but it can be in a range. For example: {beginDate: {year: 2016, month: 11, day: 14}, endDate: {year: 2016, month: 11, day: 20}}. To reset existing disableDateRanges value set empty array to it.
disableHeaderButtonstruebooleanPrevent to change the calendar view with header buttons if previous or next month are fully disabled by disableUntil or disableSince.
showWeekNumbersfalsebooleanAre week numbers visible or not on calendar. Can be used if firstDayOfWeek = mo.
selectorHeight232pxstringSelector height.
selectorWidth252pxstringSelector width.
inlinefalsebooleanShow mydaterangepicker in inline mode.
showClearDateRangeBtntruebooleanIs clear date range button shown or not. Can be used if inline = false.
height34pxstringmydatepicker height without selector. Can be used if inline = false.
width100%stringmydatepicker width. Can be used if inline = false.
selectionTxtFontSize14pxstringSelection area font size. Can be used if inline = false.
alignSelectorRightfalsebooleanAlign selector right. Can be used if inline = false.
indicateInvalidDateRangetruebooleanIf user typed date range is not same format as dateFormat, show red background in the selection area. Can be used if inline = false.
componentDisabledfalsebooleanIs selection area input field and buttons disabled or not (input disabled flag). Can be used if inline = false.
editableDateRangeFieldtruebooleanIs selection area input field editable or not (input readonly flag). Can be used if inline = false.
showSelectorArrowtruebooleanIs selector (calendar) arrow shown or not. Can be used if inline = false.
openSelectorOnInputClickfalsebooleanOpen selector when the input field is clicked. Can be used if inline = false and editableDateRangeField = false.
ariaLabelInputFieldDate range input fieldstringAria label text of input field.
ariaLabelClearDateRangeClear date rangestringAria label text of clear date range button.
ariaLabelOpenCalendarOpen CalendarstringAria label text of open calendar button.
ariaLabelPrevMonthPrevious MonthstringAria label text of previous month button.
ariaLabelNextMonthNext MonthstringAria label text of next month button.
ariaLabelPrevYearPrevious YearstringAria label text of previous year button.
ariaLabelNextYearNext YearstringAria label text of next year button.
  • Example of the options data (not all properties listed):
    myDateRangePickerOptions: IMyDrpOptions = {
        dateFormat: 'dd.mm.yyyy',
        firstDayOfWeek: 'mo',
        sunHighlight: true,
        height: '34px',
        width: '260px',
        inline: false,
        alignSelectorRight: false,
        indicateInvalidDateRange: true
    };

defaultMonth attribute

When the calendar is opened, it will ordinarily default to selecting the current date range. If you would prefer a different year and month to be the default for a freshly chosen date picking operation, specify a defaultMonth attribute.

Value of the defaultMonth attribute can be:

  • IMyDefaultMonth object. The value of defMonth property can be a string which contain year number and month number separated by delimiter. The delimiter can be any special character. For example: 08-2016 or 08/2016.
  • a string which contain year number and month number separated by delimiter. The delimiter can be any special character. For example: 08-2016 or 08/2016.

placeholder attribute

Placeholder text in the input field.

Callbacks

dateRangeChanged callback

  • called when the date range is selected, removed or input field typing is valid
  • event parameter:
    • event.beginDate: Date object in the following format: { day: 22, month: 11, year: 2016 }
    • event.beginJsDate: Javascript Date object of begin date
    • event.endDate: Date object in the following format: { day: 23, month: 11, year: 2016 }
    • event.endJsDate: Javascript Date object of end date
    • event.formatted: Date range string: '2016-11-22 - 2016-11-23'
    • event.beginEpoc: Epoc time stamp number: 1479765600
    • event.endEpoc: Epoc time stamp number: 1479852000
  • event parameter type is IMyDateRangeModel

  • Example of the dateChanged callback:

        onDateRangeChanged(event: IMyDateRangeModel) {
            console.log('onDateRangeChanged(): Begin date: ', event.beginDate, ' End date: ', event.endDate);
            console.log('onDateRangeChanged(): Formatted: ', event.formatted);
            console.log('onDateRangeChanged(): BeginEpoc timestamp: ', event.beginEpoc, ' - endEpoc timestamp: ', event.endEpoc);
        }

inputFieldChanged callback

  • called when the value change in the input field, date range is selected or date range is cleared (can be used in validation, returns true or false indicating is date range valid or not in the input field)
  • event parameter:
    • event.value: Value of the input field. For example: '2016-11-22 - 2016-11-23'
    • event.dateRangeFormat: Date range format string. For example: 'yyyy-mm-dd - yyyy-mm-dd'
    • event.valid: Boolean value indicating is the typed value valid. For example: true
  • event parameter type is IMyInputFieldChanged

  • Example of the input field changed callback:

    onInputFieldChanged(event: IMyCalendarViewChanged) {
      console.log('onInputFieldChanged(): Value: ', event.value, ' - dateRangeFormat: ', event.dateRangeFormat, ' - valid: ', event.valid);
    }

calendarViewChanged callback

  • called when the calendar view change (year or month change)
  • event parameter:
    • event.year: Year number in calendar. For example: 2016
    • event.month: Month number in calendar. For example: 11
    • event.first: First day of selected month and year. Object which contain day number and weekday string. For example: {number: 1, weekday: "tu"}
    • event.last: Last day of selected month and year. Object which contain day number and weekday string. For example: {number: 30, weekday: "we"}
  • event parameter type is IMyCalendarViewChanged
  • values of the weekday property are same as values of the firstDayOfWeek option

  • Example of the calendar view changed callback:

    onCalendarViewChanged(event: IMyCalendarViewChanged) {
      console.log('onCalendarViewChanged(): Year: ', event.year, ' - month: ', event.month, ' - first: ', event.first, ' - last: ', event.last);
    }

dateSelected callback

  • called when the date (begin or end) is selected
  • event parameter:
    • event.type: Type of selected date (begin or end). 1 = begin date, 2 = end date
    • event.date: Date object in the following format: { day: 23, month: 11, year: 2016 }
    • event.formatted: Formatted date based on dateFormat option
    • event.jsdate: Javascript Date object of the selected date
  • event parameter type is IMyDateSelected

  • Example of the date selected callback:

      onDateSelected(event: IMyDateSelected) {
          console.log('onDateSelected(): Value: ', event);
      }

inputFocusBlur callback

  • called when the input box get or lost focus
  • event parameter:

    • event.reason: Reason of the event:
      • 1 = focus to input box
      • 2 = focus out of input box
    • event.value: Value of input box
    • event parameter type is IMyInputFocusBlur
  • Example of the input focus blur callback:

    onInputFocusBlur(event: IMyInputFocusBlur): void {
        console.log('onInputFocusBlur(): Reason: ', event. reason, ' - Value: ', event.value);
    }

Change styles of the component

The styles of the component can be changed by overriding the existing styles.

Create a separate stylesheet file which contain the changed styles. Then import the stylesheet file in the place which is after the place where the component is loaded.

The sampleapp of the component contain an example:

Development of this component

  • At first fork and clone this repo.

  • Install all dependencies:

    1. npm install
    2. npm install --global gulp-cli
  • Build the npmdist folder and execute tslint:

    1. gulp all
  • Execute unit tests and coverage (output is generated to the test-output folder):

    1. npm test
  • Run sample application:

    1. Open a terminal and type npm start
    2. Open http://localhost:5000 to browser
  • Build a local npm installation package:

    1. gulp all
    2. cd npmdist
    3. npm pack
    • local installation package is created to the npmdist folder. For example: mydaterangepicker-1.0.10.tgz
  • Install the local npm package to your project:

    1. npm install path_to_npmdist/mydaterangepicker-1.0.10.tgz

Demo

Online demo is here

Compatibility (tested with)

  • Firefox
  • Chrome
  • Chromium
  • Edge
  • IE11
  • Safari

License

  • License: MIT

Author

  • Author: kekeh

Keywords

  • Date range picker
  • Angular2+
  • typescript
9.0.0

4 years ago

4.2.1

6 years ago

4.2.0

6 years ago

4.1.12

6 years ago

4.1.11

7 years ago

4.1.10

7 years ago

4.1.9

7 years ago

4.1.8

7 years ago

4.1.7

7 years ago

4.1.6

7 years ago

4.1.5

7 years ago

4.1.4

7 years ago

4.1.3

7 years ago

4.1.2

7 years ago

4.1.1

7 years ago

4.1.0

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.1.8

7 years ago

2.1.7

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

2.1.6

7 years ago

2.1.5

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.6

7 years ago

1.3.5

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago