Shivam Pandya
Shivam Pandya

Reputation: 1061

Jquery Datepicker, Increment Of Year from Number Field

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

Answers (3)

Ranjith Kumar Nagiri
Ranjith Kumar Nagiri

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

tik27
tik27

Reputation: 2698

You can use the unary operator to turn your numberofDaysToAdd to a number.

year =  date.getFullYear() + +numberOfDaysToAdd;

Upvotes: 0

Troy Wardle
Troy Wardle

Reputation: 53

Fiddle

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

Related Questions