提交 | 用户 | 时间
|
58d006
|
1 |
# Date Range Picker for Twitter Bootstrap |
A |
2 |
|
|
3 |
![Improvely.com](http://i.imgur.com/LbAMf3D.png) |
|
4 |
|
|
5 |
This date range picker component for Twitter 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 |
**[View a demo](http://www.dangrossman.info/2012/08/20/a-date-range-picker-for-twitter-bootstrap/)** |
|
15 |
|
|
16 |
## Usage |
|
17 |
|
|
18 |
This component relies on [Twitter Bootstrap](http://twitter.github.com/bootstrap/), |
|
19 |
[Moment.js](http://momentjs.com/) and [jQuery](http://jquery.com/). |
|
20 |
|
|
21 |
Separate stylesheets are included for use with Bootstrap 2 or Bootstrap 3. |
|
22 |
|
|
23 |
Basic usage: |
|
24 |
|
|
25 |
``` |
|
26 |
<script type="text/javascript" src="jquery.js"></script> |
|
27 |
<script type="text/javascript" src="moment.js"></script> |
|
28 |
<script type="text/javascript" src="daterangepicker.js"></script> |
|
29 |
<link rel="stylesheet" type="text/css" href="bootstrap.css" /> |
|
30 |
<link rel="stylesheet" type="text/css" href="daterangepicker-bs3.css" /> |
|
31 |
|
|
32 |
<script type="text/javascript"> |
|
33 |
$(document).ready(function() { |
|
34 |
$('input[name="daterange"]').daterangepicker(); |
|
35 |
}); |
|
36 |
</script> |
|
37 |
``` |
|
38 |
|
|
39 |
The constructor also takes an optional options object and callback function. The function will be called whenever |
|
40 |
the selected date range has been changed by the user, and is passed the start and end dates (moment date objects) as |
|
41 |
parameters. |
|
42 |
|
|
43 |
```` |
|
44 |
$('input[name="daterange"]').daterangepicker( |
|
45 |
{ |
|
46 |
format: 'YYYY-MM-DD', |
|
47 |
startDate: '2013-01-01', |
|
48 |
endDate: '2013-12-31' |
|
49 |
}, |
|
50 |
function(start, end) { |
|
51 |
alert('A date range was chosen: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD')); |
|
52 |
} |
|
53 |
); |
|
54 |
```` |
|
55 |
|
|
56 |
## Options Reference |
|
57 |
|
|
58 |
`startDate`: (Date object, moment object or string) The start of the initially selected date range |
|
59 |
|
|
60 |
`endDate`: (Date object, moment object or string) The end of the initially selected date range |
|
61 |
|
|
62 |
`minDate`: (Date object, moment object or string) The earliest date a user may select |
|
63 |
|
|
64 |
`maxDate`: (Date object, moment object or string) The latest date a user may select |
|
65 |
|
|
66 |
`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) |
|
67 |
|
|
68 |
`showDropdowns`: (boolean) Show year and month select boxes above calendars to jump to a specific month and year |
|
69 |
|
|
70 |
`showWeekNumbers`: (boolean) Show week numbers at the start of each week on the calendars |
|
71 |
|
|
72 |
`timePicker`: (boolean) Allow selection of dates with times, not just dates |
|
73 |
|
|
74 |
`timePickerIncrement`: (number) Increment of the minutes selection list for times (i.e. 30 to allow only selection of times ending in 0 or 30) |
|
75 |
|
|
76 |
`timePicker12Hour`: (boolean) Use 12-hour instead of 24-hour times, adding an AM/PM select box |
|
77 |
|
|
78 |
`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 |
|
79 |
|
|
80 |
`opens`: (string: 'left'/'right') Whether the picker appears aligned to the left or to the right of the HTML element it's attached to |
|
81 |
|
|
82 |
`buttonClasses`: (array) CSS class names that will be added to all buttons in the picker |
|
83 |
|
|
84 |
`applyClass`: (string) CSS class string that will be added to the apply button |
|
85 |
|
|
86 |
`cancelClass`: (string) CSS class string that will be added to the cancel button |
|
87 |
|
|
88 |
`format`: (string) Date/time format string used by moment when parsing or displaying the selected dates |
|
89 |
|
|
90 |
`separator`: (string) Separator string to display between the start and end date when populating a text input the picker is attached to |
|
91 |
|
|
92 |
`locale`: (object) Allows you to provide localized strings for buttons and labels, and the first day of week for the calendars |
|
93 |
|
|
94 |
## License |
|
95 |
|
|
96 |
This code is made available under the same license as Twitter Bootstrap. Moment.js is included in this repository |
|
97 |
for convenience. It is available under the [MIT license](http://www.opensource.org/licenses/mit-license.php). |
|
98 |
|
|
99 |
-- |
|
100 |
|
|
101 |
Copyright 2012-2013 Dan Grossman |
|
102 |
|
|
103 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
104 |
you may not use this file except in compliance with the License. |
|
105 |
You may obtain a copy of the License at |
|
106 |
|
|
107 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
108 |
|
|
109 |
Unless required by applicable law or agreed to in writing, software |
|
110 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
111 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
112 |
See the License for the specific language governing permissions and |
|
113 |
limitations under the License. |