Search code examples
javascriptjquerydatemomentjsdaterangepicker

Date calculation problem due to change of year in momentJS


I have used moment.js to calculate 30 days limit in a form field where I have used daterangepicker.js to pick the date. I have disabled all previous date from current date and all dates after 30 days counting from current date. Everything was working fine until the "year" nation attacked(hehe git it?).

My date calculation was working fine using moment.js, but as the year is changing, everything breaks. Whenever I change the year to 2021 from 2020 every date becomes disabled. Like I want to post something today(22/12/2020) and want to set the deadline next year, to do that when I change the year every date becomes disable. Before changing the year I can see the dates of next year enabled and I can select it, it works fine like that. But whenever I change the year dates becomes disabled. This is my current code to calculate date limit:

$('input[name="application_deadline"]').daterangepicker({

locale: {
  format: 'DD-MM-YYYY HH:mm'
},
singleDatePicker: true,
showDropdowns: true,
startDate: moment(),
endDate: moment().subtract(-29, 'days'),
minDate: moment(),
maxDate: moment().subtract(-29, 'days')
});

My code technically works if I disable the line: maxDate: moment().subtract(-29, 'days')

Then every date is enabled after the current date but removes the 30 days limit which I require to be working.

I have attached two screenshots of before and after changing year for the reference. Before changing the year

After changing the year


Solution

  • What you're describing sounds like a bug in daterangepicker.js. To reproduce, I've tested latest version from their website using jQuery 3.5.1.

    Unfortunately I'm not able to reproduce what you're describing. Did you try updating to latest version 3.1?