Angular date range picker
Highly configurable Angular date range picker. Compatible with Angular2 and Angular4 versions.
Online demo is here
To install this component to an external project, follow the procedure:
-
npm install mydaterangepicker --save
-
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 {}
-
If you are using systemjs package loader add the following mydaterangepicker properties to the System.config:
(function (global) { System.config({ paths: { 'npm:': 'node_modules/' }, map: { // Other components are here... 'mydaterangepicker': 'npm:mydaterangepicker/bundles/mydaterangepicker.umd.js' }, packages: { } }); })(this);
Use one of the following three options.
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 {
private 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: Object = {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>
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 {
private 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 setValue function
let date = new Date();
this.myForm.setValue({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 setValue function
this.myForm.setValue({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>
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 {
private 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>
Value of the options attribute is a type of IMyDrpOptions. It can contain the following properties.
Option | Default | Type | Description |
---|---|---|---|
dayLabels | {su: 'Sun', mo: 'Mon', tu: 'Tue', we: 'Wed', th: 'Thu', fr: 'Fri', sa: 'Sat'} | IMyDayLabels | Day 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' } | IMyMonthLabels | Month labels visible on the selector. |
dateFormat | yyyy-mm-dd | string | Date format on the selection area and the callback. For example: dd.mm.yyyy, yyyy-mm-dd, dd mmm yyyy (mmm = Month as a text) |
showClearBtn | true | boolean | Show the 'Clear' button on calendar. |
showApplyBtn | true | boolean | Show the 'Apply' button on calendar. |
showSelectDateText | true | boolean | Show the select date text. |
selectBeginDateTxt | Select Begin Date | string | Select begin date text. Can be used if showSelectDateText = true. |
selectEndDateTxt | Select End Date | string | Select end date text. Can be used if showSelectDateText = true. |
firstDayOfWeek | mo | string | First day of week on calendar. One of the following: mo, tu, we, th, fr, sa, su |
sunHighlight | true | boolean | Sunday red colored on calendar. |
markCurrentDay | true | boolean | Is current day (today) marked on calendar. |
markCurrentMonth | true | boolean | Is current month marked on calendar. Can be used if monthSelector = true. |
markCurrentYear | true | boolean | Is current year marked on calendar. Can be used if yearSelector = true. |
monthSelector | true | boolean | If month label is selected opens a selector of months. |
yearSelector | true | boolean | If year label is selected opens a selector of years. |
minYear | 1100 | number | Minimum allowed year in calendar. Cannot be less than 1100. |
maxYear | 9100 | number | Maximum allowed year in calendar. Cannot be more than 9100. |
disableUntil | no default value | IMyDate | Disable 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} |
disableSince | no default value | IMyDate | Disable 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} |
disableDates | no default value | Array<IMyDate> | 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. |
enableDates | no default value | Array<IMyDate> | 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. |
disableDateRanges | no default value | Array<IMyDateRange> | 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. |
disableHeaderButtons | true | boolean | Prevent to change the calendar view with header buttons if previous or next month are fully disabled by disableUntil or disableSince. |
showWeekNumbers | false | boolean | Are week numbers visible or not on calendar. Can be used if firstDayOfWeek = mo. |
selectorHeight | 232px | string | Selector height. |
selectorWidth | 252px | string | Selector width. |
inline | false | boolean | Show mydaterangepicker in inline mode. |
showClearDateRangeBtn | true | boolean | Is clear date range button shown or not. Can be used if inline = false. |
height | 34px | string | mydatepicker height without selector. Can be used if inline = false. |
width | 100% | string | mydatepicker width. Can be used if inline = false. |
selectionTxtFontSize | 14px | string | Selection area font size. Can be used if inline = false. |
alignSelectorRight | false | boolean | Align selector right. Can be used if inline = false. |
indicateInvalidDateRange | true | boolean | If user typed date range is not same format as dateFormat, show red background in the selection area. Can be used if inline = false. |
componentDisabled | false | boolean | Is selection area input field and buttons disabled or not (input disabled flag). Can be used if inline = false. |
editableDateRangeField | true | boolean | Is selection area input field editable or not (input readonly flag). Can be used if inline = false. |
showSelectorArrow | true | boolean | Is selector (calendar) arrow shown or not. Can be used if inline = false. |
openSelectorOnInputClick | false | boolean | Open selector when the input field is clicked. Can be used if inline = false and editableDateRangeField = false. |
ariaLabelInputField | Date range input field | string | Aria label text of input field. |
ariaLabelClearDateRange | Clear date range | string | Aria label text of clear date range button. |
ariaLabelOpenCalendar | Open Calendar | string | Aria label text of open calendar button. |
ariaLabelPrevMonth | Previous Month | string | Aria label text of previous month button. |
ariaLabelNextMonth | Next Month | string | Aria label text of next month button. |
ariaLabelPrevYear | Previous Year | string | Aria label text of previous year button. |
ariaLabelNextYear | Next Year | string | Aria 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
};
Provide the initially chosen date range that will display both in the text input field and provide the default for the popped-up selector.
Type of the selDateRange attribute can be a string or an IMyDateRange object.
- the string must be in the following format dateFormat - dateFormat option is. For example '2018-10-09 - 2018-10-19'
- the object must be in the IMyDateRange format. For example: {beginDate: {year: 2018, month: 10, day: 9}, endDate: {year: 2018, month: 10, day: 19}}
If selDateRange is not specified, when the calendar is opened, it will ordinarily default to selecting the current date. 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 text in the input field.
-
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);
}
-
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);
}
-
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);
}
-
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);
}
-
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
- event.reason: Reason of the event:
-
Example of the input focus blur callback:
onInputFocusBlur(event: IMyInputFocusBlur): void {
console.log('onInputFocusBlur(): Reason: ', event. reason, ' - Value: ', event.value);
}
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:
- override.css contain the changed styles.
- index.html contain import of the override.css file.
-
At first fork and clone this repo.
-
Install all dependencies:
- npm install
- npm install --global gulp-cli
-
Build the npmdist folder and execute tslint:
- gulp all
-
Execute unit tests and coverage (output is generated to the test-output folder):
- npm test
-
Run sample application:
- Open a terminal and type npm start
- Open http://localhost:5000 to browser
-
Build a local npm installation package:
- gulp all
- cd npmdist
- 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:
- npm install path_to_npmdist/mydaterangepicker-1.0.10.tgz
Online demo is here
- Firefox (latest)
- Chrome (latest)
- Chromium (latest)
- Edge
- IE11
- Safari
- License: MIT
- Author: kekeh
- Date range picker
- Angular2
- Angular4