How to check date is change in date range picker?


I am using Bootstrap Date Range Picker and i want that if user change date then i submit form for fresh data. Here is my code,

$(document).ready(function (){
    var start = moment().subtract(29, 'days');
    var end = moment();

    function cb(start, end) {
        $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

        startDate: start,
        endDate: end,
        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')]
    }, cb);

    cb(start, end);

and my form is
enter image description here
how can i do that. Thanks


Duplicate? I guess this is your way anwser for your question.

$('#reportrange').on('apply.daterangepicker', function(ev, picker) {
    alert ('hello');

Either way, you can submit your form when the date changed using your callback (cb), by adding some trigger to submit form, such as: $("#someform").submit(); Make sure that you are inserting the selected date in each change to form input.

Moreover, in daterangepicker examples. I think you`ll find there your solution aswell.

I took your code and just change span to input and html to val and there you go.

<script type="text/javascript">
  $(document).ready(function () {
    var start = moment().subtract(29, "days");
    var end = moment();

    function cb(start, end) {
      $("#reportrange input").val(
        start.format("MMMM D, YYYY") + " - " + end.format("MMMM D, YYYY")

        startDate: start,
        endDate: end,
        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": [
          "Last Month": [
            moment().subtract(1, "month").startOf("month"),
            moment().subtract(1, "month").endOf("month")

    cb(start, end);

Answered By – Alon Albahari

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.