Bootstrap Daterange picker for angular2 and 4
npm install angular-2-daterangepicker:warning: If you are on angular v6 or later. Please consider using angular-datetimerangepicker package
Daterangepicker for Angular vX.X. Although the name of package is angular 2 it is compatible with angular v4 and v5
It is a fully responsive daterangepicker with or without bootstrap.css. See responsive section below for more details.
This module is strictly intended to work on browsers only and not in node/browserless environments
This is a work in progress and you are always welcome to help me going forward with this project.
bash
$ npm install angular-2-daterangepicker
`
or`bash
$ bower install angular-2-daterangepicker
`Demo
see Demo
or Plunker to how to consume this module or You can play around with the code on stackblitz hereUsage
How to make it work for you
Import DaterangepickerModule into your module as following`ts
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { DaterangepickerModule } from 'angular-2-daterangepicker';
@NgModule({
imports: [BrowserModule, DaterangepickerModule],
declarations: [ AppComponent ],
bootstrap: [AppComponent]
})
export class AppModule {
}
`Options
Currently, very minimum number of options are available but I will keep on developing and adding more and more options
Option Name
Type
Purpose
Default Value
Possible Values
format
string
format that this daterangepicker will use to communicate with your code
"YYYY-MM-DD"
As per momentjs standard formats
displayFormat
string
format that will be displayed to end user
Same as format
As per momentjs standard formats
startDate
string
Default start date when this components is rendered for first time. Format of this date should be in line with format option's value above
Current Systetm Date
date string in line with format option's value above
endDate
string
Default end date when this components is rendered for first time. Format of this date should be in line with format option's value above
Current Systetm Date
date string in line with format option's value above
minDate
string
Default minimum date not including this date. End user will not be able select all dates before this date. Format of this date should be in line with format option's value above
null
date string in line with format option's value above
maxDate
string
Default maximum date not including this date. End user will not be able select all dates after this date. Format of this date should be in line with format option's value above
null
date string in line with format option's value above
inactiveBeforeStart
boolean
Blurs all dates before selected start date. So end user can not select toDate to be before fromDate.
false
true,false
autoApply
boolean
Removes apply and cancel buttons and applies as soon as user selects end date
false
true,false
showRanges
boolean
Predefined ranges to show to end user. So end user has ready options instead of navingating through calendars.
false
true,false
preDefinedRanges
Array of object shown as below.
Custom ranges if you want to define your own ranges. This is useful only if showRanges option is set to true.
see below for more details
see below for more details
noDefaultRangeSelected
boolean
This option set the startDate and endDate options to blank on first render.This date range picker sets startDate and endDate to be current system date by dafault if no value is passed to startDate and endDate.
false
true,false
singleCalendar
boolean
Use only one calendar. So you do not need another datepicker for single month.
false
true,false
position
string
position of the flyout which will open. By default it opens on left edge of input box
'left'
'left','right','center'
disabled
boolean
Whether to disable the main input control
false
true,false
timePicker
object
Whether to show timepicker
null
Object explained as below
disableBeforeStart
boolean
Whether to disable dates that ar before selected start date in right calendar. This option applies to right calendar only
false
true,false
alwaysOpen
boolean
Whether to keep the calendars always open. This option removes the main input box where range is shown
false
true,false
$3
For custom range, Pass options as below. For this you need to pass momentjs objects.
`js
preDefinedRanges: [{
name: 'Day After tomorrow',
value: {
start: moment().add(2, 'days'),
end: moment().add(2, 'days'),
}
},{
name: 'This week',
value: {
start: moment(),
end: moment().add(7, 'days'),
}
}]
`All dates are supposed to be string and in format as you are passing.
You can also
`ts
import { Options } from 'angular-2-daterangepicker';
`
class for passing options to the component.$3
Timepicker options expects an object containing following keys as timepicker options
Option Name
Type
Purpose
Default Value
Possible Values
minuteInterval
(integer)number
The interval by which minutes will increase/decrease when user changes minutes in timepicker
1
anything between 1 to 59. If you supply value greater or equal 60 then that value mod 60 is taken as actual value
Events
Subscribe to rangeSelected event as
`html
`
the event listener will receive a javascript object conaining
`js
{
start: 'moment object representing start date selected by user'
end: 'moment object representing end date selected by user'
}
`
and if you have set singleCalendar to true then the event listener will receive following
`js
{
start: 'moment object representing date selected by user'
}
`
How pass options to the component
The input box automatically takes class of the date-range-picker tag`ts
import { Component } from '@angular/core';@Component({
selector: "my-datepicker-demo",
template:
})
export class AppComponent{
daterangepickerOptions = {
startDate: '09/01/2017',
endDate: '09/02/2017',
format: 'DD/MM/YYYY'
}
}`Dependencies
moment.js version greater than 2.17.1
moment-range.js version 2.2.0
also you should have installed @types/node or see here for more information.
I suggest installing all the dependencies before this moduleResponsive CSS
If you are using bootstrap.css then just include the following styling in your code
if you do not want to include whole bootstrap.css then include this css in your code.
`html
``