Reputation: 1061
I use this code for datepicker and want to increment of Year (Only Year from full date), which display on other field.. Currently It append number (type=number not working in firefox, good with chrome) field value to current year.
I want that if 5 in number field then it should display (2014 + 5) = 2019. But My code return 20145 as Year
Here Is Fiddle
My Js Code Is Here.
$( "#dtp" ).datepicker({
minDate: 0,
onSelect: function(dateText, inst) {
var date = $(this).datepicker('getDate'),
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear() + 1;
//alert(day + '-' + month + '-' + year);
$('#popupDatepicker1').val(day + '-' + month + '-' + year);
}
});
$('#user_lic').change(function(){
var numberOfDaysToAdd = this.value;
var date = $('#dtp').datepicker('getDate'),
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear() + numberOfDaysToAdd;
$('#popupDatepicker1').val(day + '-' + month + '-' + year);
})
Upvotes: 0
Views: 997
Reputation: 863
you can use this code:
$( "#dtp" ).datepicker({
minDate: 0,
onSelect: function(dateText, inst) {
var date = $(this).datepicker('getDate'),
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear() + 1;
//alert(day + '-' + month + '-' + year);
$('#popupDatepicker1').val(day + '-' + month + '-' + year);
}
});
$('#user_lic').change(function(){
var numberOfDaysToAdd = parseInt(this.value);
var date = $('#dtp').datepicker('getDate'),
day = date.getDate(),
month = date.getMonth() + 1,
year = parseInt(date.getFullYear() + numberOfDaysToAdd);
$('#popupDatepicker1').val(day + '-' + month + '-' + year);
})
Upvotes: 0
Reputation: 2698
You can use the unary operator to turn your numberofDaysToAdd
to a number
.
year = date.getFullYear() + +numberOfDaysToAdd;
Upvotes: 0
Reputation: 53
The numberOfDaysToAdd variable is being saved as a string, so it's being concatenated rather than added. Turn it into a float and this will work fine.
var days2add = parseFloat(numberOfDaysToAdd);
Upvotes: 2