Javascript: when it’s midnight add +1 on date

Asked

Viewed 213 times

-1

My date when it arrives midnight continues the day before, I wanted it to be for the next day (current).

For example:

At 23:50 on the 6th, he shows Saturday, 6 October 2018, 00:00

Since midnight is already the next day, I want you to stay: Sunday, 7 October 2018, 00:00

PS: I can’t program c(:

<html>
    <head>
        <script language="javascript" type="text/javascript">
            var d = new Date();
            now = new Date;
            monName = new Array ("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro");
            dayName = new Array ("Domingo","Segunda-feira","Terça-feira","Quarta-feira","Quinta-feira","Sexta-feira","Sábado");
            hourName = new Array (00,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,"00");
        </script>
    </head>
    <body>
        <script language="javascript" type="text/javascript">
            document.write ("<center><span style='width: 50%; font-size: 18px; line-height: 24px; color: #000000;'>" + dayName [now.getDay()] + ", " + now.getDate() + " de " + monName [now.getMonth()] + " de " + now.getFullYear() + ", às " + hourName [now.getHours()+1] + ":00" + " </span></center>")
        </script>
    </head>
</html>
  • Where is the timer? I only see a date being written on the page.

  • Exactly, it’s not a timer, just a date to be written, like I said, I can’t program. I just want you to stay October 7 instead of October 6, for example, when the writing arrives at midnight (00:00).

  • Dude I believe that using a timer helps in creating this code, I’ll be leaving some steps for you to perform - Create a counter that checks the time - Add +1 to the day counter whenever the time is 00:00

1 answer

0

Guy I highly recommend that you see and dig into:

Javascript Timing Events

You can take a look at this link: https://www.w3schools.com/js/js_timing.asp

Down here I’ll be putting a code (reverse to what you want) that makes a countdown

// Set the date we're counting down to / Seta a data inicial para contagem regressiva
var countDownDate = new Date("Oct 6, 2018 23:59:59").getTime();

// Update the count down every 1 second / atualiza o contador a cada 1s
var x = setInterval(function() {

    // Get todays date and time / pega a data e hora de agora
    var now = new Date().getTime();
    
    // Find the distance between now and the count down date / acha a distancia da data atual e do contador 
    var distance = countDownDate - now;
    
    // Time calculations for days, hours, minutes and seconds / calcula o dia horas minutos e segundos que faltam
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
    // Output the result in an element with id="demo" / mostra na tela dentro do elemento com id = "demo"
    document.getElementById("demo").innerHTML = days + "d " + hours + "h "
    + minutes + "m " + seconds + "s ";
    
    // If the count down is over, write some text / quando o contador chegar em 0 apresenta "expirado"
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
p {
  text-align: center;
  font-size: 60px;
  margin-top: 0px;
}
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p id="demo"></p>
</body>
</html>

Browser other questions tagged

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