Add days to Javascript Date () in the format dd / mm / yyyy [duplicate]

2

Hello, How do I show the date generated in the Brazilian format? I searched here and on the internet but could not execute as I need.

See:

$(document).ready(function () {
	$("#button").click(function() {
        
        var dias = 2;
        var dataAtual = new Date();
        var previsao = dataAtual.setDate(dataAtual.getDate() + dias);  		
		
		$("#dPrev").val(dataAtual);
        
        
		
	});	
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script><inputname="dPrev" type="text" class="form-control" id="dPrev" value=""  placeholder="DD/MM/YYYY" required >

    
    <button type="submit" id="button" name="button" class="btn btn-success pull-right"></i> Salvar</button>

It takes the current date, adds days and needs to display in the field as DD / MM / YYYY

Thanks for any help.

    
asked by anonymous 19.06.2015 / 16:32

2 answers

4

One way to solve this problem would be as follows:

var dias = 2;
var dataAtual = new Date();
var previsao = dataAtual.setDate(dataAtual.getDate() + dias);       

var dataBrasil = previsao.getDate() + "//" + previsao.getMonth() + "//" + previsao.getFullYear();

("#dPrev").val(dataBrasil);

Another way would be to add a method to the Date class:

Date.prototype.toDataBrasil = function () {
    return this.getDate() + '//' + this.getMonth() + '//' + this.getFullYear();
};

And then use it like this:

var dataBrasil = previsao.toDataBrasil();
    
19.06.2015 / 16:42
1

$(document).ready(function () {
	$("#button").click(function() {
        
        var dias = 2;
        var dataAtual = new Date();            
        var previsao = new Date();

        previsao.setDate(dataAtual.getDate() + dias);  		
        n = previsao.getDate()  +"/" + (previsao.getMonth() + 1)+ "/" + previsao.getFullYear();
		$("#dPrev").val(n);
	});	
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script><inputname="dPrev" type="text" class="form-control" id="dPrev" value=""  placeholder="DD/MM/YYYY" required >

    
    <button type="submit" id="button" name="button" class="btn btn-success pull-right"></i> Salvar</button>
    
19.06.2015 / 16:41