Date calculation and onblur tag

Asked

Viewed 132 times

1

I’m creating a screen to add orders. When I choose the date of sale, I would like it to automatically calculate the due date and exchange the value of the maturity field (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>

There are the fields, but I have no idea how to solve this problem. Someone can help me?

If this is of any importance, both date fields open a calendar, but do not restrict the user from writing 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'});

  • Why not create a Javascript function and invoke it in the event onblur of the element DATA_VENDA?

  • Unfortunately I tried but for some reason onblur did not work, so I would like an example

  • 1

    What makes the function mascara_data? You can put it in the question?

  • Added the question, I forgot to add!

1 answer

3


Probably the date typed in DATA_VENDA will be in the Brazilian standard (day/month/year), and not in the American one (month/day/year). Soon, you will need to treat this value before providing it to the variable "dataVenda" (I believe your mask function already does this treatment).

The following script will associate the Novadata function to 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;

}
  • You forgot to define the hoje, I guess the place could be dataVenda, nay?

  • 1

    That’s right, I refactored and ended up forgetting the previous name of the variable.

  • Your solution is good, but unfortunately it doesn’t work, when I open the screen, in the field DADA_VENC_EBS appears as follows Nan /Nan /Nan

  • Then there must be some error when the variables dataVenda and vencEbs are created. You can try adding "console.log(variable);" just below to see what’s going on. If you can assemble a jsfiddle with the form it is easier.

  • Okay, just take a look at the post again, I put together the input data, if anything helps. I am testing here, looking for the errors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.