-2

I'm trying to calculate date by selecting number of months to shows result date with -1 day.

 $('#txtjQcal15_1, #month_number').change(function(){ 
 var months = +$('#month_number').val();    
 var endmDate = new Date($('#txtjQcal15_1').val()); 
 if (isNaN(endmDate)) {endmDate = 0;} 
  else {
 endmDate.setMonth(endmDate.getMonth() + Number(months) ); 
 $('#date_result_month').val( endmDate.getFullYear() + '-' + ( "0" + (endmDate.getMonth() + 1)).slice(-2) + '-' + ("0" + (endmDate.getDate() - 1)).slice(-2) ) ;

if i select 2016-09-09 result shows 2016-10-08 and its okay.

Only Calculation for begging of the month is wrong for example if i select 2016-09-01 the result shows:2016-10-00 !!! instead of 2016-09-30

allogos
  • 47
  • 2
  • 11

1 Answers1

0

Adding endmDate.setDate(endmDate.getDate() - 1); Solved my problem.

$('#txtjQcal15_1, #month_number').change(function(){ 
var months = +$('#month_number').val();    
var endmDate = new Date($('#txtjQcal15_1').val()); 
if (isNaN(endmDate)) {endmDate = 0;} 
 else {
endmDate.setMonth(endmDate.getMonth() + Number(months) ); 
endmDate.setDate(endmDate.getDate() - 1);
$('#date_result_month').val( endmDate.getFullYear() + '-' + ( "0" + (endmDate.getMonth() + 1)).slice(-2) + '-' + ("0" + endmDate.getDate()).slice(-2) )  ;
allogos
  • 47
  • 2
  • 11