Add days to Javascript Date() in dd/mm/yyyy format

Asked

Viewed 8,912 times

2

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

Behold:

$(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>




<input name="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 show in the field as DD/MM/YYYYA

I appreciate any help.

  • Tiago, a good option is to use a plugin like Moment js. i like working with it. I recommend. ;)

2 answers

4

One of the ways to solve this problem would be like this:

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 her like this:

var dataBrasil = previsao.toDataBrasil();
  • The current date has to add with the variable dias

  • Yes, but I left that sum in the reply

  • Does not work, see http://jsfiddle.net/5n73cL2j/2/

  • @Tiago is only some syntax errors: http://jsfiddle.net/5n73cL2j/3/

  • @Kaduamaral Ball Show. But as it is done to show the full Tada, when it has 0, does not show, see http://jsfiddle.net/5n73cL2j/4/, would have to show 01/06/2015

  • @Kaduamaral can help me with the above question to finish?

  • @Kaduamaral, a detail I noticed. The Month is getting as last date. Today is month 06, in the code this showing month 05

  • Can you help me? The date is going wrong, see http://jsfiddle.net/5n73cL2j/6/

  • 1

    @James is returning 5 because he starts counting from 0. So January = 0, and December = 11. Here example working http://jsfiddle.net/odex98qj/

  • @Tiago http://jsfiddle.net/5n73cL2j/7/

  • Notice that add up like this: dataAtual.getDate() + dias It won’t work because he won’t adjust when it’s the end of the month. You need to turn to Time, and then add up the days, then get the date added up with getDate()

Show 6 more comments

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>




<input name="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>

  • You have to add the current date to the variable dias

  • But you have to show as DD/MM/YYYY

  • Can you help me? The date is going wrong, see http://jsfiddle.net/5n73cL2j/6/

  • so you don’t break your head low and into the variable put so: $("#dPrev").val( moment().format('MMM D, YYYY') );

  • +1 our reduced to 2 Lines only. Update your code to qualify. http://jsfiddle.net/5n73cL2j/8/

Browser other questions tagged

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