Javascript clock with NTP time or server

4

I have the code below, which displays a clock on a page, which serves to record factory intervals. It works perfectly, with the code below:

function moveRelogio(){

    momentoAtual = new Date();

    hora = momentoAtual.getHours();

    if (hora < 10) {
      hora = '0' + hora;
    } else {
      hora = hora + '';
    }

    minuto = momentoAtual.getMinutes();
    if (minuto < 10) {
      minuto = '0' + minuto;
    } else {
      minuto = minuto + '';
    }

    segundo = momentoAtual.getSeconds();
    if (segundo < 10) {
      segundo = '0' + segundo;
    } else {
      segundo = segundo + '';
    }

    horaImprimivel = hora + ":" + minuto + ":" + segundo

    document.cadastro.hora_inicio.value = horaImprimivel;

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

Eventually the displayed time is not the same registered, because for registration the AD time is used. The user can not change it because PI is used without keyboard and mouse.

Is there any way to get the time from a server or the internet from an NTP?

    
asked by anonymous 20.06.2017 / 20:49

2 answers

1

Using the same logic as the code I had, I changed the code and now it picks up the time of the server that is hosting the code. It worked perfectly:

<?php
//PEGA HORA ATUAL DO SERVIDOR
date_default_timezone_set('Etc/GMT');
$hoje = getdate();
?>
<script>
    var d = new Date(Date.UTC(<?php echo $hoje['year'].",".$hoje['mon'].",".$hoje['mday'].",".$hoje['hours'].",".$hoje['minutes'].",".$hoje['seconds']; ?>));
    setInterval(function() {
        d.setSeconds(d.getSeconds() + 1);
        //EXIBE O HORÁRIO COM 2 DIGITOS
        $('#hora_inicio').val((("0" + d.getHours()).slice(-2) +':' + ("0" + d.getMinutes()).slice(-2) + ':' +  ("0" + d.getSeconds()).slice(-2) ));
    }, 1000);
</script>

And in HTML:

      <tr>
          <td>Hora Atual:</td>
          <td><input type = "text" style="font-family: Verdana, Geneva, sans-serif; height: 40px; font-size: 30px;" readonly="true" id="hora_inicio" name = "hora_inicio" size = 12></td>
      </tr>

Final:

    
21.06.2017 / 14:16
4

If you are going to use an NTP for business use, you will probably have to pay for a license.

If you use a platform on the server that allows you to use PHP, .NET, Java, Ruby etc. you can set up a service that returns local and UTC times.

To consume, it would look something like the code below. Example in jQuery because I suppose you are not a masochist:

var dt;
$.ajax({
    url: "http://enderecoDoSeuServico/",
    success: function (horario) {
        dt = new Date(horario);
    }
});

For this to work, it suffices that the variable horario is the number of milliseconds since zero hour on January 1, 1970 (in the Greenwhich timezone), which is the value "zero" of type Date of Javascript. For that reason, the .NET code could look something like:

public long RetornaHorario()
{
    DateTime inicio = new DateTime(1970, 1, 1); // supondo fuso horário zero
    TimeSpan span = DateTime.Now - inicio;
    return span.TotalMilliseconds;
}

It should not be very different with the other languages that you can use on the server.

Remember that there will be a latency between the call and the return of the service, so you will have to accept a precision of a few milliseconds to a few seconds depending on your setup.

    
20.06.2017 / 21:08