-2

I have an input with some date value and I want to sum 60 days to that that and put that val into other input. How can I do that?

Something like 2012-12-17 in first input and 2013-02-15 in second input

<td width="148"><input name="USER_joindate" id="USER_joindate" type="text" readonly="readonly" value="2012-12-17"></td>

<td><input name="EndPeriodExperience" id="EndPeriodExperience" type="text" readonly="readonly"></td>



$( document ).ready(function() {
   $('#USER_joindate').on('change', function() {

....magic ... 


   });
});
user3810795
  • 162
  • 1
  • 17

2 Answers2

0

download moment.js from http://momentjs.com/ and try this:

$(document).ready(function() {
   $('#USER_joindate').on('change', function() {
       // get the value of USER_joindate
       var dateString = $(this).val();

       // validate the date format inside USER_joindate
       if (dateString.match(/^[0-9]{2}-[0-9]{2}-[0-9]{4}$/g)) {
           // create a new date object using moment.js
           var dateObj = moment(dateString);
           // add 60 days to the date
           dateObj.add(60, 'days');
           // fill EndPeriodExperience with the new date
           $("#EndPeriodExperience").val(dateObj.format("YYYY-MM-DD"));
       }
   });
});
0

I just find the way, check the fiddle:

$( document ).ready(function() {
    $("#USER_joindate").on("change", function(){
       var date = new Date($("#USER_joindate").val()),
           days = parseInt($("#days").val(), 10);

        if(!isNaN(date.getTime())){
            date.setDate(date.getDate() + 61);
            //2012-12-17
            $("#EndPeriodExperience").val(date.toInputFormat());
        } else {
            alert("Fecha Invalida");
            $("#USER_joindate").focus();
        }
    });

    Date.prototype.toInputFormat = function() {
       var yyyy = this.getFullYear().toString();
       var mm = (this.getMonth()+1).toString(); 
       var dd  = this.getDate().toString();
       return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]);
    };
});

Thanks to all for your answers!

user3810795
  • 162
  • 1
  • 17