How to add the amount of days with jQuery

0

I'm trying to manipulate dates with jQuery. The idea is as follows:

The user informs Data_vencimento and type (in radio button ), whether the payment will be weekly, biweekly, monthly or informed.

In% with% entered, the user will enter the number of days (for example, 20 days) so that jQuery can calculate the maturities.

Example:

  • The user reports the due date: 11/14/2018;
  • User selects weekly earnings;
  • jQuery computes and fills the value of input with date 11/21/2018. (14 + 7).
  • Only input #dt_venc1 returns a alert(dtVenc1) .

    How do I manipulate dates with jQuery? It seems that manipulating with dates with PHP is a lot easier. But with jQuery, it would be independent of the language used.

    $(document).ready( function () {
        $('#data_vencimento').on('blur', function() {
            alert('Usuário utilizando o input "DATA_VENCIMENTO"');
            var dtVenc = new Date();
            var dtVenc1 = new Date();
            dtVenc = $('#data_vencimento').val();
            dtVenc1 = dtVenc1.setDate(dtVenc1 + 7);
            alert(dtVenc);
            alert(dtVenc1);
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="row">
         <div class="form-group col-md-2">
              <label class="control-label" for="data_vencimento">Data Vencimento *</label>
              <input type="text" class="form-control datepicker required" name="data_vencimento" id="data_vencimento">
         </div>
    
         <div class="form-group col-md-2">
              <label class="control-label" for="data_prorrogacao">Data Prorrogacao</label>
              <input type="text" class="form-control datepicker" name="data_prorrogacao" id="data_prorrogacao">
         </div>
    </div>
    
    <div class="row">
         <div class="form-check form-check-radio form-check-inline">
              <label class="form-check-label" >Tipo de Parcelamento:</label>
              	<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="SEMANAL"> Semanal
              	<span class="circle">
                    	<span class="check"></span>
              	</span>
              </label>
         </div>
         <div class="form-check form-check-radio form-check-inline">
              <label class="form-check-label">
                    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="QUINZENAL" checked="checked"> Quinzenal
                         <span class="circle">
                               <span class="check"></span>
                         </span>
              </label>
         </div>
              <div class="form-check form-check-radio form-check-inline">
                   <label class="form-check-label">
                         <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="MENSAL" checked="checked"> Mensal
                                <span class="circle">
                                      <span class="check"></span>
                                </span>
                   </label>
         </div>
         <div class="form-check form-check-radio form-check-inline">
              <label class="form-check-label">
                   <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio4" value="INFORMADO"> Informado
                       <span class="circle">
                             <span class="check"></span>
                       </span>
              </label>                           
         </div>
         <div class="col-md-2">
              <div class="form-group has-label">
                   <label class="bmd-label-floating" for="dias">Qtd. dias</label>
                   <input class="numero form-control" type="text" name="dias" size="03" 
                          id="dias">
              </div>
         </div>
    </div>
        
    asked by anonymous 12.11.2018 / 13:55

    3 answers

    0

    You're almost right ..

    instead of using:

    dtVenc1 = dtVenc1.setDate(dtVenc1 + 7);
    

    Try:

    dtVenc1 = dtVenc1.setDate(dtVenc1.getDate() + 7);
    
        
    12.11.2018 / 14:20
    0

    errors are the use of the same variable in the calculation dtVenc1 and the lack of getDate() without getDate () its variable is just a string and its sum will result in something like Mon Nov 19 2018 18:02:09 GMT+0300 (GMT+03:00)7 a 7 at the end of the text.

    If you use:

    dtVenc1.setDate(dtVenc.getDate() + 7);
    

    You will get this value: Mon Nov 19 2018 18:02:09 GMT+0300 (GMT+03:00)

    If you use

    dtVenc1 = dtVenc1.setDate(dtVenc.getDate() + 7);
    

    You will get this value: 1542639729006

        
    12.11.2018 / 16:06
    0

    I would use the MomentJS library to work with dates / hours, follow the link:

    link

    In my opinion the best solution for complex calculations using these formats.

        
    12.11.2018 / 16:38