Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the real UTC timestamp (client independent)?

Is there a way to get the real current UTC timestamp without being dependent on the client's time which might not be set correctly on every client?

I know with JavaScript's getTime() I can get the UTC timestamp in milliseconds. It's independent of the client's timezone, but I think it's dependent of the client's current time.

I'm trying to make a live clock that shows the real UTC time, so it shouldn't matter if the client's time is set correctly or not for it to be correct.

like image 820
Kid Diamond Avatar asked Dec 31 '25 04:12

Kid Diamond


1 Answers

Get this JSON:

http://json-time.appspot.com/time.json

Or

http://www.timeapi.org/utc/now

Also you can get it from your own server if you have one.

For an example of the first one (with interval):

HTML:

<div id="time">Click the button</div>
<button id="gettime">Get the time</button>

JAVASCRIPT (With JQuery):

$("#gettime").click(function() {
setInterval(ajaxCalltoGetTime, 1000);
});


function ajaxCalltoGetTime() {
$.ajax({
type: "GET",
    url: 'http://json-time.appspot.com/time.json',
    dataType: 'jsonp'
})
.done(function( msg ) {
    $("#time").html(msg.hour + ":" + msg.minute + ":" + msg.second );
});

JSFiddler: http://jsfiddle.net/op8xdsrv/

like image 93
Leandro Bardelli Avatar answered Jan 01 '26 19:01

Leandro Bardelli