Calculating date and tag onblur

1

I'm creating a screen to add orders. When I choose the date of the sale, I would like it to automatically calculate the due date and change the maturity field value (which is always 180 days after the date of purchase).

<table>
    <tr>
        <td align='left'>&nbsp;Valor do Pedido : </td>
        <td align='left'><input style="width: 115px;" id="VALOR_PEDIDO" required value="" name="VALOR_PEDIDO" type='text' maxlength='100' tabindex='5' size='30'/></td>
        <td align='left'>&nbsp;Data da venda : </td>
        <td align='left'><input style="width: 115px;" id="DATA_VENDA" required value="" name="DATA_VENDA" type="us-date0" OnKeyUp="mascara_data(this.value)" type='text' maxlength='10' tabindex='5' size='30'/></td>
        <td align='left'>&nbsp;Data de vencimento EBS : </td>
        <td align='left'><input style="width: 115px;" id="DATA_VENC_EBS" required value="" name="DATA_VENC_EBS" type="us-date0" OnKeyUp="mascara_data(this.value)" type='text' maxlength='10' tabindex='5' size='30'/></td>
    </tr>
</table>

Here are the fields, but I have no idea how to solve this problem. Can anyone help me?

If this is of any importance, both date fields open a calendar, but do not restrict the user from typing in the field.

                        var month = (new Date()).getMonth() + 1;
                        var year = (new Date()).getFullYear();
                        $('input[type=us-date0]').w2field('date', {format: 'dd/mm/yyyy', placeholder: 'dd/mm/aaaa'});
    
asked by anonymous 05.06.2015 / 17:27

1 answer

3

Probably the date entered in DATA_VENDA will be in Brazilian standard (day / month / year), and not American (month / day / year). So, you'll need to treat this value before giving it to the variable "dataVend" (I believe your mask function already does this treatment).

The following script will associate the newData function with the onblur event of the DATA_VENDA input:

document.getElementById('DATA_VENDA').onblur = novaData();

function novaData(){

    var dataVenda = new Date( document.getElementById('DATA_VENDA').value );

    var vencEbs= new Date(new Date(dataVenda).setMonth(dataVenda.getMonth()+6));

    var dia = vencEbs.getDate()    
    var mes = vencEbs.getMonth()+1; //janeiro é 0
    var ano = vencEbs.getFullYear(); 

  document.getElementById('DATA_VENC_EBS').value = dia+' /'+mes+' /'+ano;

}
    
05.06.2015 / 19:51