Error returning date

Asked

Viewed 113 times

1

Eae you guys, beauty?

I have a problem here, everything was working normally and suddenly started this problem.

Follows below 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 he’s returning the date this way:

20/000/2017 16:08:26

The day and time is right, just giving problem in the month and year. It keeps updating straight on the site, IE, fica correr os segundos la....

Does anyone know what it might be? Some function that replaces getMonth and getFullYear?

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

  • 1

    I would like to see what is coming in this digital variable. Anyway I would draw the line if (mes == 0) mes = "0" + mes;

  • var digital = new Date(<?php echo str_replace(':',', ', str_replace('/',', ', str_replace(' ',', ', $Hora_Servidor_MySQL))).', 0'; ?>);

  • I tried to take that line but it didn’t work...

  • Take the two lines that treats the month to see how the dispTime variable is formatted.

  • Just decreased the zeroes, was like this: 20/0/2017 16:32:54

  • Show what is coming in the variable $Hora_Servidor_MySQL

  • $Hora_Servidor_MySQL = date('Y/m/d H:i:s');

  • The time is displayed by a div, thus: <div id="horarioServidor"><div class="horarioRelogio"></div></div>

  • Yes, it starts from zero until 11, being 0 for January and 11 for December, this I saw in the document... But you say change where exactly?

  • You have to change the month to subtract 1 before moving to new Date(), and change again by incrementing 1 before passing the content to the div, it’s easier to follow Pedro’s solution.

  • Can you pass me the way you said? Because I couldn’t do it the other way.

Show 6 more comments

1 answer

0

I think you don’t need all this just to format, you can use the function toLocaleDateString and format using the second parameter.

I’ve formatted as you wish, but take a look at the documentation that 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);  
}
  • Boy, I have no knowledge in that area so I won’t know how to apply my rsrs code...

  • Thank you Pedro. But it takes away a doubt, I believe that this occurring only in the month of December, why in the previous months it showed the date normally... Does it make sense or nothing to see?

  • Analyzing your code, when you use the digital.getMonth() should show 11, why Javascript Date starts counting from scratch. Try giving a console.log on the date coming from the server to see what is showing.

  • I tried to use the code you gave me, and he didn’t return anything, it was blank..

  • Show any errors in the console? Debugged code? Left blank can say a lot, it is difficult to help. =/

  • On the console, the following appeared: Typeerror: ({}) is not a Function and Referenceerror: formatData is not defined

  • Fixed the code from a look, but the message already tells everything that was occurring. =/

  • Just changed the format by formatting Date right? I did it, really, but it was blank the same way huahuahua.

  • now: Typeerror: data is Undefined

  • The variable you are passing to the function formatarData is worth undefined. That is to say, digital is undefined.

  • I was able to correct, but the date appears this way: 20/01/2017 17:57:58

  • The digital variable ta with the correct value now, at least in the source code it displays the correct date, but it ta printing for the following month....

  • var digital = new Date(2016, 12, 20, 17, 57, 45, 0);

  • The months begin to count from 0, see the documentation

  • I have read this documentation, but I could not understand what should be changed exactly to display the correct date...

  • I must change the digital variable?

Show 11 more comments

Browser other questions tagged

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