2
I have a dynamic form and made a function to somar 30 dias
after the first due date, but insert dates having the day 31
or day 01
is making the calculations wrong, for example:
if I enter 31/12/2017 as being 1st maturity, in 3 installments should be like this
1º 31/12/2017
2º 30/01/2018
3º 01/03/2018
But it’s coming out like this:
1º 31/01/2018
2º 31/01/2018
3º 31/03/2018
Below is a functional example.
$(function() {
$('#form_forn').submit(function() {
$('.table-result').show();
$('#result-table tbody tr').remove();
var vencimento = $('#txtVencimento').val().split("/");
var qtde_parcelas = $('#txtParcelas').val();
var x = 1;
var acc_vlr_total = 0;
var acc_vlr_pop = 0;
vencimento = new Date(vencimento[2] + "/" + vencimento[1] + "/" + vencimento[0]);
var data_sum = 1;
while (x <= qtde_parcelas) {
var data = adicionarDiasData(data_sum, vencimento);
$('#result-table tbody').append("<tr id=\"row_nf-" + x + "\" class=\"nf\"><td>" + x + " de " + qtde_parcelas + "</td><td title=\"vencimento\" class=\"text-center\">" + data + "</td></tr>");
x++;
data_sum += 30;
}
return false;
});
});
function adicionarDiasData(dias, primeiro_ven) {
//var hoje = new Date();
var dia = new Date(primeiro_ven.getTime());
var dataVenc = new Date(primeiro_ven.getTime() + (dias * 24 * 60 * 60 * 1000));
return dia.getDate() + "/" + ("0" + (dataVenc.getMonth() + 1)).slice(-2) + "/" + dataVenc.getFullYear();
//console.log(dia.getDate() + "/" + ("0" + (dataVenc.getMonth() + 1)).slice(-2) + "/" + dataVenc.getFullYear());
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form_forn" class="form-horizontal" name="novolancamento" method="post" action="#" autocomplete="off">
<fieldset>
<legend>Cadastrar novo lançamento Fornecedor</legend>
<div class="container">
<div class="row">
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="txtVencimento">1º Vencimento:</label>
<div class="col-md-8">
<input id="txtVencimento" name="txtVencimento" type="date" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="txtParcelas">Quantidade parcelas:</label>
<div class="col-md-8">
<input id="txtParcelas" name="txtParcelas" type="number" placeholder="" class="form-control input-md" title="Valor compreendido entre 1 e 12" required="" maxlength="2" min="1" max="12">
</div>
</div>
</div>
<div class="row">
<div class="col-md-offset-8 col-md-4">
<!-- Button (Double) -->
<div class="form-group">
<div class="col-md-12">
<button type="submit" id="btnSalvar" name="btnSalvar" class="btn btn-warning pull-right">Simular lançamento</button>
</div>
</div>
</div>
</div>
</fieldset>
</form>
<hr class="hr">
<br/>
<div class="table-result" style="display: none;">
<table id="result-table" class="table table-hover table-condensed">
<thead>
<tr>
<th>Parcela</th>
<th class="text-center">Vencimento</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<br>
<hr>
<button class="btn btn-success" style="float:right; margin-right: 6%;" id="salvar_lancamento">Salvar</button>
<br>
<br>
Already tried to modify its function to increase 1 month instead of 30 days?
– Carlos Andrade
@Carlosandrade, so I’m actually having trouble understanding the date function, I would have some example to inform me, another thing I forgot to mention is that on the return of the dates the days from 1 to 9 return without the 0 (zero) to the left getting like this
1/01/2018
– WMomesso