Click here to Skip to main content
15,166,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
$("#txtcredit_periodform").on("change paste keyup", function () {
           // alert($(this).val());
           debugger;
           var someDate = new Date($('#txt_invdateform').val());
           var numberOfDaysToAdd = $(this).val();
           someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
           var date = someDate.getMonth() + '/' + someDate.getDate() + '/' + someDate.getFullYear();
           $('#txtcredit_dateform').val(date);


       });


What I have tried:

I want to add days to selected date which will reflect in another textbox as new date. though the code is working but calculation goes wrong. please help
Posted
Updated 26-Feb-17 23:39pm
Comments
Michael_Davies 27-Feb-17 4:39am
   
What do you mean by the code works but the calculation goes wrong, show an example.
Member 10549697 27-Feb-17 4:53am
   
it takes the date into mm dd yy format.i don't know what exactly happens.but it shows wrong date

1 solution

I suspect that numberOfDaysToAdd is a string... then if, for example, someDate.getDate() is 27 and numberOfDaysToAdd is "5", then someDate.getDate() + numberOfDaysToAdd) will be "275" rather than 32.

Parse the value to an integer before adding it up to the getDate() return value:
JavaScript
var numberOfDaysToAdd = parseInt($(this).val(), 10);
if (isNaN(numberOfDaysToAdd)) {
  // invalid number; show an error message
  return;
}
// continue with the calculations
   
Comments
Member 10549697 27-Feb-17 5:07am
   
Correct sir!
this was the problem. now resolved.
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900