Error returning date

1

Are you guys, beauty?

I have a problem here, everything was working normally and nothing started to give me this problem.

Here is the function:

function atualizaHoraServidor() {

    var dia = digital.getDate();
    var mes = digital.getMonth();
    var ano = digital.getFullYear();
    var horas = digital.getHours();
    var minutos = digital.getMinutes();
    var segundos = digital.getSeconds();

    // aumenta 1 segundo
    digital.setSeconds(segundos + 1);

    // acrescento zero
    if (dia <= 9) dia = "0" + dia;
    if (mes <= 9) mes = "0" + mes;
    if (mes == 0) mes = "0" + mes;
    if (horas <= 9) horas = "0" + horas;
    if (minutos <= 9) minutos = "0" + minutos;
    if (segundos <= 9) segundos = "0" + segundos;

    dispTime = dia + "/" + mes + "/" + ano + " " + horas + ":" + minutos + ":" + segundos;
    $('#horarioServidor .horarioRelogio').text(dispTime);
    setTimeout("atualizaHoraServidor()", 1000); // chamo a função a cada 1 segundo

}

What happens is that it is returning the date like this:

20/000/2017 16:08:26

The day and time is right, it's only giving you trouble in the month and year. It keeps updating directly in the site, that is, it is running the seconds the ....

Does anyone know what it can be? Any function that overrides getMonth and getFullYear?

I do not know if I'm wrong, but I believe this only occurs for the month of DECEMBER, the previous month was normal.

    
asked by anonymous 20.12.2016 / 19:10

1 answer

0

I think you do not need all this just to format, you can use the toLocaleDateString and format using the second parameter.

I have formatted as you wish, but have a look at the documentation you understand.

var dataAtual = new Date();
var options = {
  year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: '2-digit'
};
var dataFormatada = dataAtual.toLocaleDateString('pt-br', options);

console.log(dataFormatada);

Now, how would that look with your code:

function atualizaHoraServidor() {
  var dispTime = formatarData(digital);

  $('#horarioServidor .horarioRelogio').text(dispTime);

  digital.setSeconds(digital.getSeconds() + 1);

  setTimeout("atualizaHoraServidor()", 1000);
}

function formatarData(data) {
  var options = {
    year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: '2-digit'
};
  return data.toLocaleDateString('pt-br', options);  
}
    
20.12.2016 / 19:51