Mojito
Mojito

Reputation: 31

How to make bootstrap datepicker pick last day of the month instead of first day when minViewMode : 1

I'm using this version of Bootstrap DatePicker: http://eternicode.github.io/bootstrap-datepicker/ (the range type), and i have uploaded my demo on JSFiddle on here: http://jsfiddle.net/qs5co179/6/

The relevant JavaScript:

$('#sandbox-container .input-daterange').datepicker({
    format: "dd/mm/yyyy",
    minViewMode : 1
});

I have set the parameter minViewMode : 1 so I can view only months on calendar instead of full days, the reason is that I want to strict the user to enter:

For example my data entry that i am looking for like below:

From: 01/07/2015 - To: 30/09/2015.. in this format (dd/mm/yyyy)

Now I can't find any parameter to set the calender to pick the last day of the month instead of always 1st day when you click on the month in "To:" field.

one more thing also that I want to always make "From:" and "To:" having 3months period and no less, I want always to make the range 3 months minimum for the entery.

Thanks a lot in advance...

Upvotes: 1

Views: 2156

Answers (1)

meskobalazs
meskobalazs

Reputation: 16041

You could attach an event handler to the changeDate event (doc), and change the date manually (i.e. add a month, substract a day).

For the date handling part, I would

  • Get the date parts
  • Increase the month by one, if its December, then change it to January
  • Create a new Date instance
  • date = date.setDate(date.getDate() - 1) to get the correct day

The date parts could be exctracted by simple string slicing:

var day = str.slice(0,2);
var month = str.slice(3,5);
var year = str.slice(6,10);

Upvotes: 1

Related Questions