Reputation: 4069
I have a legacy web app that is using some old version of bootstrap datepicker. I've uploaded the entire datepicker.js file here. https://pastebin.com/aYzMrWgW
I am trying to set my Start Date picker up so that when it changes, I get the value selected and calculate the last day of the same month chosen, and set that value as the value for End Date.
I am using the code below to get the value from the Start Date picker $('#dp1')
and attempting to set it to the value of the End Date picker $('#dp1a')
. When I check the console, my lastDay
variable is correct. If I choose 8/1/2017 - it logs out the string representation of 8/31/2017 and son on. However I can not get it to update the End Date with the correct value.
$('#dp1').datepicker().on('changeDate', function(ev) {
$('.datePickerStart').change();
});
$('.datePickerStart').change(function() {
// Parse the date and get the last day of the month selected
var date = new Date($(this).val());
var lastDay = new Date(date.getFullYear(), date.getMonth() + 2, 0);
console.log(lastDay);
$('#dp1a').datepicker('setValue', lastDay);
});
If I call setValue as I am above, it sets the value to 9/1/2017 instead.
can anyone point out what i'm doing wrong please?
Upvotes: 1
Views: 2160
Reputation: 3426
your bootstrap datepicker version is too old and it doesn't seems to have the complete method .datepicker('setValue', value)
.There are some line of code missing when compared to current version.You can check difference here.
Even though i have made some changes in your code to setValue using your version.
$(document).ready(function(){
var second = $('#dp1a').datepicker().data('datepicker');
$('#dp1').datepicker().on('changeDate', function(ev) {
$('.datePickerStart').change();
});
$('.datePickerStart').change(function() {
// Parse the date and get the last day of the month selected
var date = new Date($(this).val());
var lastDay = new Date(date.getFullYear(), date.getMonth() + 2, 0);
console.log(lastDay);
second.date=lastDay;
second.setValue();
});
});
But your code works perfect when updated with current version.Its always better to work with latest versions to avail these methods easily.
Hope this helps!
Upvotes: 1