Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
# Date Range Picker for Bootstrap
2
 
3
![Improvely.com](http://i.imgur.com/LbAMf3D.png)
4
 
5
This date range picker component for Bootstrap creates a dropdown menu from which a user can
6
select a range of dates. I created it while building the UI for [Improvely](http://www.improvely.com),
7
which needed a way to select date ranges for reports.
8
 
9
If invoked with no options, it will present two calendars to choose a start
10
and end date from. Optionally, you can provide a list of date ranges the user can select from instead
11
of choosing dates from the calendars. If attached to a text input, the selected dates will be inserted
12
into the text box. Otherwise, you can provide a custom callback function to receive the selection.
13
 
14
The component can also be used as a single date picker by setting the `singleDatePicker` option to `true`.
15
 
16
**[View a demo](http://www.dangrossman.info/2012/08/20/a-date-range-picker-for-twitter-bootstrap/)** or **[Try it in a live application](https://awio.iljmp.com/5/drpdemogh)**
17
 
18
## Usage
19
 
20
This component relies on [Bootstrap](http://getbootstrap.com),
21
[Moment.js](http://momentjs.com/) and [jQuery](http://jquery.com/).
22
 
23
Separate stylesheets are included for use with Bootstrap 2 or Bootstrap 3.
24
 
25
Basic usage:
26
 
27
```
28
<script type="text/javascript" src="jquery.js"></script>
29
<script type="text/javascript" src="moment.js"></script>
30
<script type="text/javascript" src="daterangepicker.js"></script>
31
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
32
<link rel="stylesheet" type="text/css" href="daterangepicker-bs3.css" />
33
 
34
<script type="text/javascript">
35
$(document).ready(function() {
36
  $('input[name="daterange"]').daterangepicker();
37
});
38
</script>
39
```
40
 
41
The constructor also takes an optional options object and callback function. The function will be called whenever
42
the selected date range has been changed by the user, and is passed the start and end dates (moment date objects)
43
and the predefined range label chosen (if any), as parameters. It will not fire if the picker is closed without
44
any change to the selected dates.
45
 
46
````
47
$('input[name="daterange"]').daterangepicker(
48
  {
49
    format: 'YYYY-MM-DD',
50
    startDate: '2013-01-01',
51
    endDate: '2013-12-31'
52
  },
53
  function(start, end, label) {
54
    alert('A date range was chosen: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
55
  }
56
);
57
````
58
 
59
## Options
60
 
61
`startDate`: (Date object, moment object or string) The start of the initially selected date range
62
 
63
`endDate`: (Date object, moment object or string) The end of the initially selected date range
64
 
65
`minDate`: (Date object, moment object or string) The earliest date a user may select
66
 
67
`maxDate`: (Date object, moment object or string) The latest date a user may select
68
 
69
`dateLimit`: (object) The maximum span between the selected start and end dates. Can have any property you can add to a moment object (i.e. days, months)
70
 
71
`showDropdowns`: (boolean) Show year and month select boxes above calendars to jump to a specific month and year
72
 
73
`showWeekNumbers`: (boolean) Show week numbers at the start of each week on the calendars
74
 
75
`timePicker`: (boolean) Allow selection of dates with times, not just dates
76
 
77
`timePickerIncrement`: (number) Increment of the minutes selection list for times (i.e. 30 to allow only selection of times ending in 0 or 30)
78
 
79
`timePicker12Hour`: (boolean) Use 12-hour instead of 24-hour times, adding an AM/PM select box
80
 
81
`ranges`: (object) Set predefined date ranges the user can select from. Each key is the label for the range, and its value an array with two dates representing the bounds of the range
82
 
83
`opens`: (string: 'left'/'right') Whether the picker appears aligned to the left or to the right of the HTML element it's attached to
84
 
85
`buttonClasses`: (array) CSS class names that will be added to all buttons in the picker
86
 
87
`applyClass`: (string) CSS class string that will be added to the apply button
88
 
89
`cancelClass`: (string) CSS class string that will be added to the cancel button
90
 
91
`format`: (string) Date/time format string used by moment when parsing or displaying the selected dates
92
 
93
`separator`: (string) Separator string to display between the start and end date when populating a text input the picker is attached to
94
 
95
`locale`: (object) Allows you to provide localized strings for buttons and labels, and the first day of week for the calendars
96
 
97
`singleDatePicker`: (boolean) Show only a single calendar to choose one date, instead of a range picker with two calendars; the start and end dates provided to your callback will be the same single date chosen
98
 
99
`parentEl`: (string) jQuery selector of the parent element that the date range picker will be added to, if not provided this will be `'body'`
100
 
101
## Functions
102
 
103
Several functions are provided for updating the picker's option and state after initialization:
104
 
105
`setOptions(object, function)`: This function has the same signature and purpose as the date range picker's constructor: it sets the picker's options to their defaults, overrides them with any values in an options object you provide, and sets the callback for selection changes to whatever function you provide
106
 
107
`setStartDate(Date/moment/string)`: Sets the date range picker's currently selected start date to the provided date
108
 
109
`setEndDate(Date/moment/string)`: Sets the date range picker's currently selected end date to the provided date
110
 
111
Example usage:
112
 
113
````
114
//create a new date range picker
115
$('#daterange').daterangepicker({ startDate: '2014-03-05', endDate: '2014-03-06' });
116
 
117
//change the selected date range of that picker
118
$('#daterange').data('daterangepicker').setStartDate('2014-03-01');
119
$('#daterange').data('daterangepicker').setEndDate('2014-03-31');
120
````
121
 
122
## Events
123
 
124
Several events are triggered on the element you attach the picker to, which you can listen for:
125
 
126
`show.daterangepicker`: Triggered when the picker is shown
127
 
128
`hide.daterangepicker`: Triggered when the picker is hidden
129
 
130
`apply.daterangepicker`: Triggered when the apply button is clicked
131
 
132
`cancel.daterangepicker`: Triggered when the cancel button is clicked
133
 
134
Some applications need a "clear" instead of a "cancel" functionality, which can be achieved by changing the button label and watching for the cancel event:
135
 
136
````
137
$('#daterange').daterangepicker({
138
  locale: { cancelLabel: 'Clear' }
139
});
140
 
141
$('#daterange').on('cancel.daterangepicker', function(ev, picker) {
142
  //do something, like clearing an input
143
  $('#daterange').val('');
144
});
145
````
146
 
147
While passing in a callback to the constructor is the easiest way to listen for changes in the selected date range, you can also do something every time the apply button is clicked even if the selection hasn't changed:
148
 
149
````
150
$('#daterange').daterangepicker();
151
$('#daterange').on('apply.daterangepicker', function(ev, picker) {
152
  console.log(picker.startDate.format('YYYY-MM-DD'));
153
  console.log(picker.endDate.format('YYYY-MM-DD'));
154
});
155
````
156
 
157
## License
158
 
159
This code is made available under the same license as Bootstrap. Moment.js is included in this repository
160
for convenience. It is available under the [MIT license](http://www.opensource.org/licenses/mit-license.php).
161
 
162
--
163
 
164
Copyright 2012-2014 Dan Grossman
165
 
166
Licensed under the Apache License, Version 2.0 (the "License");
167
you may not use this file except in compliance with the License.
168
You may obtain a copy of the License at
169
 
170
   http://www.apache.org/licenses/LICENSE-2.0
171
 
172
Unless required by applicable law or agreed to in writing, software
173
distributed under the License is distributed on an "AS IS" BASIS,
174
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
175
See the License for the specific language governing permissions and
176
limitations under the License.