How to add another day using "new date()"

Asked

Viewed 482 times

2

Hello, I am with this function, but I do not know how to put an extra day in the date, someone can help me.

var myVar = setTimeout(function(){ myTimer() });

function myTimer() {
    var d = new Date();
    var options = {weekday: 'long', month: 'long', year: 'numeric', day: 'numeric' }
    var t = d.toLocaleDateString('pt-br', (options));
        document.getElementById("demo").innerHTML = t;

}
  • My string type can be considered different?

  • Hello Leo, where exactly is this string? The part of adding one day or more is well explained in the question that Linkei, don’t you agree? The string you refer to is coming from the server?

  • I’m starting now, I thought this question and the answers are easier, and the string I say and the way it is displayed on the screen. " Saturday, 1 de July, 2017".

  • But your question does not talk about how to display on the screen and neither do the answers of Renan and Matthew speak of this, in fact your code already does the display part, the doubt only seems even about "add", if it is for a detail or other almost every question I ask that has a ; different would no longer be duplicate, don’t you agree? ;) ... Now if your problem is also to display, this would have to be described in the body of the question, does not agree?

  • I agree, but that question will be erased?

  • Leo duplicates are not banned questions or evil or anything, are closed only to prevent receiving answers and avoid decentralizing the subject and are still linked to the main question which is great to help users search for various answers in the different questions. There’s nothing to worry about.... I highly recommend reading this link: https://pt.meta.stackoverflow.com/a/2676/3635 ;D

  • Vlw, very beginner yet, rsrsrs

Show 2 more comments

2 answers

2


The canonical way, cross-browswer, documented and that works anywhere is you add the amount of milliseconds a day to your date.

I.and.:

var agora = new Date();
var agoraAmanha = new Date(agora.getTime() + (1000 * 60 * 60 * 24));
// essa é a quantidade de milissegundos em 24 horas.

The reason is that:

  • Adding one to the date is browser behavior. It is not guaranteed that all browsers will have the same behavior, nor that the same browser will have the same behavior on all devices. Outside other environments like Node.js. I’m not saying it doesn’t work in every environment you use... I just find the above solution more guaranteed.

  • You could also use the method setDate Date type. In the environments I tested, adding one day does what you want. Setar the day 32 March, for example, causes you to get a date on the first of April. But once again, there’s no guarantee of having this behavior in every environment.

  • Vlw, full answer, thank you very much.

1

Add a line:

d.setDate(d.getDate() + 1);

the code will be:

    var myVar = setTimeout(function(){ myTimer() });

    function myTimer() {
        var d = new Date();
        d.setDate(d.getDate() + 1);
        var options = {weekday: 'long', month: 'long', year: 'numeric', day: 'numeric' }
        var t = d.toLocaleDateString('pt-br', (options));
            document.getElementById("demo").innerHTML = t;

    }

You don’t have to worry about the last day of the month or the year, because the Javascript date is intelligent in terms of rollover.

  • Thanks for the answer, I really help.

  • Remember you can also check if there are no answers on the site on the subject and instead of answering you could use Flag and mark with duplicate of for example https://answall.com/q/9281/3635, so you can help us to collaborate with the organization of the site ;D

  • Vlw by the tip William.

Browser other questions tagged

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