longbill / jquery-date-range-picker

A jQuery plugin that allows user to select a date range
MIT License
1.12k stars 579 forks source link

DateRangePicker Opening Month #388

Open RunlevelConsulting opened 6 years ago

RunlevelConsulting commented 6 years ago

Using singleMonth: true

If the daterangepicker value is empty, the opening month will always be the previous month to now. Example: If we're currently in August, the daterangePicker will always default to opening in July (even if my date range doesn't include July).

If the daterangepicker has a value, the opening month will always be the "from" date month. Example, my range is from Jun 1st - Sept 30th. The daterangepicker will always open in June.


Is there a way to specify what month the daterangepicker should open on, at least for singleMonth configs? Perhaps an onOpen-type function? I would like it to open on the "to" date or the current month.

RunlevelConsulting commented 6 years ago

For anyone else suffering with this, my solution for now is to add:

customOpenAnimation: function(cb){ $(this).fadeIn(300, cb); for (i = 0; i < 100; i++) { $('.date-picker-wrapper').find('span.next').click(); } },

Replace the 100 with a suitably high number. This will simulate clicking the 'Next Month' button a bunch of times while the daterangepicker opens. It doesn't allow me to set a specific month but it does at least ensure that the last month with selectable dates is presented first.

zpassarelli commented 6 years ago

I had the same issue. Went digging through the source to see one of the API functions resetMonthsView() actually takes a date. So following the dateRangePicker config/init you can add

 $('.date-input-element').data("dateRangePicker").resetMonthsView(moment());

If you are in single month mode this will make it default to the correct month. If you are in normal mode and want the right side month to be current just modify the parameter to moment().subtract(1, 'month')

berkansahin commented 6 years ago

Thanks @zpassarelli