9

I need to obtain current time (from a credible source) using JSON. Precise time is mission-critical in my application so I cannot rely on the time of the device, even if it is only a second or two off.

EDIT: I am not as worried about 'precision' rather just so that several devices running the app have the same time.

antonpug
  • 13,724
  • 28
  • 88
  • 129
  • 1
    If you need it in JSON it kind of implies your app is running in a browser, in which case I'd be surprised if you could achieve timing precision within a second or two, considering all the async stuff going on. Or do you need JSON for another reason I'm missing? – antlersoft Feb 28 '12 at 17:02
  • 4
    Precision will be an issue anyway. An isolated request, without ping times etc, can't account for latency. – cHao Feb 28 '12 at 17:04
  • It is a phonegap android app. So everything compiles to an .apk file. So it's not "really" a web app. But I basically need to get time from a centralized server and compare it to device's time - so that I can sync results from several devices by knowing what their time difference is with the centralized time. – antonpug Feb 28 '12 at 17:06
  • agreed, < 1s precision is really impractical. by the time you request/retrieve, parse, and perform actions upon the time I'm sure you'll have lost your precision at least 1s. – Matt K Feb 28 '12 at 17:06
  • Even if the server sends a precise time, device #1 may get it faster than device #2 because device #2 might be on a slow network connection. So I'm not sure if it's truly possible to have millisecond precision. – pimvdb Feb 28 '12 at 17:09
  • You need to be looking at a proper NTP implementation, not something involving JSON (and especially not something that involves HTTP as could be implied from JSON). – Quentin Feb 28 '12 at 17:10
  • Well the problem is, with PhoneGap, I don't have much of an option but to use JS based approach. And I do realize it's not 100% precise, but it is part of what my customer wants so I want to at least implement it - still better than no time sync at all – antonpug Feb 28 '12 at 17:12
  • possible duplicate of [Getting the current GMT world time](http://stackoverflow.com/questions/489581/getting-the-current-gmt-world-time) – NimChimpsky Feb 28 '12 at 17:26

4 Answers4

6

As of Jan. 07th 2020 http://worldtimeapi.org/ is working fine. we can get current date and time details for specfic time-zone or ip address easily in either json format or plain text format.

http://worldtimeapi.org/api/timezone/America/Santiago

the above url will give you the current date and time details in json for "America/Santiago".

http://worldtimeapi.org/api/timezone/Asia/Kolkata

the above url will give you the current date and time details in json for "Asia/Kolkata".

Request the current time based on your public IP (as JSON):

$ curl "http://worldtimeapi.org/api/ip"

Note: by default, the API returns JSON. Adding a suffix of .txt to any API URL will return a plain-text response, which may be easier to parse on some systems.

Suresh P
  • 61
  • 1
  • 3
  • As of 2020-08-21, both `http://worldtimeapi.org/` and `http://worldclockapi.com` works fine. a GET request to `http://worldclockapi.com/api/json/utc/now` gives the time in json. – MWaheed Aug 21 '20 at 16:50
  • For me, that webapge loaded extremely slow. – Ambrus Tóth Aug 23 '21 at 12:39
3
function getTime(zone, success) {
    var url = 'http://json-time.appspot.com/time.json?tz=' + zone,
        ud = 'json' + (+new Date());
    window[ud]= function(o){
        success && success(new Date(o.datetime));
    };
    document.getElementsByTagName('head')[0].appendChild((function(){
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = url + '&callback=' + ud;
        return s;
    })());
}

getTime('GMT', function(time){
    // This is where you do whatever you want with the time:
    alert(time);
});

from here

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 1
    right now the server is over it's quota, so I would not rely solely on this server if it is mission-critical. Are there other similar servers? – Patrick Jackson Jan 15 '13 at 00:57
  • 1
    this one http://date.jsontest.com/ and http://time.jsontest.com/ but not I`m not shure about thers up time. – Vasil Valchev Feb 27 '14 at 13:21
0

As of Sept. 12th 2015 http://www.timeapi.org/utc/now.json seems to be working.

{"dateString":"2015-09-12T23:15:56+01:00"}

More information here http://www.timeapi.org. It's hosted on Heroku and the source is on Github.

kwabford
  • 19
  • 4
  • www.timeapi.org seems to be broken for EDT at the time of this posting. Right now a request to get [EST](http://www.timeapi.org/est/now.json) gives me `{"dateString":"2016-03-28T20:25:21-05:00"}`. A request to get [EDT](http://www.timeapi.org/edt/now.json) gives me `{"dateString":"2016-03-28T22:27:57-03:00"}`, and for reference, a request to [UTC](http://www.timeapi.org/utc/now.json) says `{"dateString":"2016-03-29T01:28:34+00:00"}`. I'm in Michigan, which should be EDT now, and the time here when I made the queries is 21:27, which is UTC-4. – skelliam Mar 29 '16 at 01:36
  • 4
    It seems `timeapi.org` is no longer available. – lepe Jun 02 '18 at 02:49
  • 1
    http://now.httpbin.org is currently working. http://httpbin.org is pretty great too. – kwabford Jun 06 '18 at 05:24
0

The worldtimeapi.org is working just fine. If you will be using Javascript:

const zone = 'Europe/Lisbon'

fetch('https://worldtimeapi.org/api/timezone/' + zone)
  .then(r => r.json())
  .then(r => {
    // strip out timezone offset from datetime ISO string
    const d = new Date(r.datetime.replace(/[+-]\d\d:\d\d$/, ''))
    console.log(`Time now in ${zone}: ${d.getHours()}:${d.getMinutes()}`)
  })
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109