daterangepicker show year and months only
Asked Answered
D

2

9

I'm using daterangepicker bootstrap 3.

$('#pa_date*').daterangepicker({
                singleDatePicker: true,
                showDropdowns: true,
                minDate: min,
                maxDate: max,
                format: 'DD/MM/YYYY'
            }).on('apply.daterangepicker', function (ev, picker) {
                alert(picker.startDate.format('MM/YYYY'));
            });

It shows me the full daterangepicker:

enter image description here

I want to hide the calendar and show only years and months drop-down, like this:

enter image description here

Doorstone answered 3/6, 2015 at 11:12 Comment(1)
I suggest - https://mcmap.net/q/1314652/-javascript-month-range-pickerGarbage
E
11

Just add the following css ...

$('input[name="daterange"]').daterangepicker({
  singleDatePicker: true,
  showDropdowns: true,
  minDate: '06/01/2013',
  maxDate: '06/30/2015',      
  format: 'DD/MM/YYYY'
}).on('hide.daterangepicker', function (ev, picker) {
  $('.table-condensed tbody tr:nth-child(2) td').click();
  alert(picker.startDate.format('MM/YYYY'));
});
.table-condensed thead tr:nth-child(2),
.table-condensed tbody {
  display: none
}
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/2.9.0/moment.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/1/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/1/daterangepicker-bs3.css" />

<input name="daterange" value="DD/MM/YYYY">
Entertainer answered 3/6, 2015 at 12:10 Comment(4)
thnaks for quick help but there is some problem it's when i select year and month the daterange don't change or the result don't give me alert message jsfiddle.net/nwjmyb9cDoorstone
Won´t work with the current version of daterangepicker. I´m currently trying to accomplish the same thing but they seem to have moved the event listener to the <div class="calendar"> so nothing fires on clicking the <td>. Any ideas?Lamori
new version seems kinda bugged ... some buttons missing and I could not make it trigger any kind of event. My tip: use the old version ;DEntertainer
There's no angular tag in the question. You should try component-styles to make the css work (angular.io/guide/component-styles)Entertainer
S
2

i'm making some change from previous answer and it work on newer version, i adding some notes to on line that important.

jsfiddle.net/game5413/snc1Lowf/2/

var td = $(instance.container).find('.table-condensed tbody tr:nth-child(3) td:first-child');
    /*
     * the setTimeout have on purpose to delay calling trigger
     * event when choosing date on third row, if you not provide
     * the timeout, it will throw error maximum callstack
     */
    setTimeout(function() {
      /*
       * on the newer version to pick some date was changed into event
       * mousedown
       */
      td.trigger('mousedown');
      /*
       * this was optional, because in my case i need send date with
       * starting day with 1 to keep backend neat
       */
      instance.setStartDate(instance.startDate.date(1));
      instance.setEndDate(instance.endDate.date(1));
      alert("this is start " + instance.startDate.format("DD MMM YYYY"));
      alert("this is end " + instance.endDate.format("DD MMM YYYY"));
    }, 1);

you can look for detail on the link i provided

Sholom answered 11/10, 2020 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.