简体   繁体   中英

Selecting 'This Month' range by default in bootstrap daterangepicker

I'm using Bootstrap Daterange Picker, which uses the following range options :

ranges: {
        'Today': [moment(), moment()],
        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
      },

But when the page loads the selected value is empty.

I want to select the range ' This Month ' by default when the page loads, in bootstrap daterangepicker.

Jsfiddle Url: https://jsfiddle.net/renishar/wnuy6ypc/

Just set the startDate and endDate options to the respective value of This month.

Also call cb function after initialise your datepicker to set the first value

See here : https://jsfiddle.net/wnuy6ypc/6

var dat_opt = {
      ...,
      startDate: moment().startOf('month'),
      endDate: moment().endOf('month'),
      ranges: {
        ...
      },
}

EDIT : After initialisation add jQuery('.daterange').trigger("apply.daterangepicker");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM