48

I am seeking a reliable REST API that can provide world time and time zone information across platforms.

I need the current time as a string. I'd like it to return the result in under a second, regardless of the user's location worldwide.

Among other implementations I want to use this for a consistent countdown timer, to be more accurate than a user's [possibly-inaccurate] computer time. It can be GMT or another time zone, as long as the time zone and offset is specified, like 2012-11-05 16:16:50 EST.

I would build this API myself, but have concerns of potential latency issues (as well as inelegance) when filtering someone through a whole big software stack like Rails just to return a simple String.

Excessive latency for users far away from the US east coast would offset the benefit of accuracy that the task requires.

Any suggestions and/or examples are appreciated.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
chadoh
  • 4,343
  • 6
  • 39
  • 64
  • 1
    This is rough, but works: `wget -q -O - www.worldtimeserver.com/time-zones/cst | grep "Server Time"`. Obviously you can change the timezone. Getting the date takes a bit more work, but it's in the page. Also you can pull the time value if you grep for serverTimeStamp tho you'll have to do some conversion obviously – kernelsmith Jan 13 '16 at 19:14
  • 7
    See also [`http://worldclockapi.com/`](http://worldclockapi.com/)... free, no registration, simple. **Examples:** [UTC Time Now](http://worldclockapi.com/api/json/utc/now) and ["Is Seattle/PST on Daylight Savings on April 20, 2019?"](http://worldclockapi.com/api/json/pst/2019-04-20). – ashleedawg Oct 18 '18 at 08:07
  • 2
    @ashleedawg: The question is still off topic. Did you miss the *Some questions are still off-topic, **even if they fit into one of the categories listed above*** part, perhaps? Questions that are not about software tools commonly used by programmers are off-topic no matter what, recommendation questions are off topic even if they are about software tools. Number of views never factor in determining if a post is off topic. – Martijn Pieters Oct 18 '18 at 13:10
  • @ashleedawg I deleted your comment, as though it was well intentioned, it was misguided. Let me know if you have any issues. –  Oct 18 '18 at 13:16
  • 1
    I believe this is what you are looking for friend: http://worldtimeapi.org/ – Zaffer Aug 18 '21 at 23:28

3 Answers3

21

TimezoneDb provides a free API: http://timezonedb.com/api

GenoNames also has a RESTful API available to get the current time for a given location: http://www.geonames.org/export/ws-overview.html.

You can use Greenwich, UK if you'd like GMT.

Aamir
  • 5,324
  • 2
  • 30
  • 47
  • 3
    Both of these suck, unfortunately. GeoNames doesn't return seconds, which I need. And I've only been able to get a successful response from TimezoneDb once, mostly it just gives me a 403 Forbidden (even though I'm definitely using the proper format and the key they gave me). I wish these worked! – chadoh Nov 06 '12 at 14:49
  • Are you using Javascript on the frontend? This is doable through Javascript without the use of additionally APIs or frameworks. I would personally go that route rather than relying on a 3rd-party API. – Aamir Nov 06 '12 at 15:08
  • 4
    I am using JavaScript. And it works, except that the user's computer time can be inaccurate by several minutes. That's why I'd prefer to get the time via a low-latency API call which is guaranteed to be accurate within the deviation of the mentioned latency. – chadoh Nov 06 '12 at 15:25
8

This API gives you the current time and several formats in JSON - https://market.mashape.com/parsify/format#time. Here's a sample response:

{
  "time": {
    "daysInMonth": 31,
    "millisecond": 283,
    "second": 42,
    "minute": 55,
    "hour": 1,
    "date": 6,
    "day": 3,
    "week": 10,
    "month": 2,
    "year": 2013,
    "zone": "+0000"
  },
  "formatted": {
    "weekday": "Wednesday",
    "month": "March",
    "ago": "a few seconds",
    "calendar": "Today at 1:55 AM",
    "generic": "2013-03-06T01:55:42+00:00",
    "time": "1:55 AM",
    "short": "03/06/2013",
    "slim": "3/6/2013",
    "hand": "Mar 6 2013",
    "handTime": "Mar 6 2013 1:55 AM",
    "longhand": "March 6 2013",
    "longhandTime": "March 6 2013 1:55 AM",
    "full": "Wednesday, March 6 2013 1:55 AM",
    "fullSlim": "Wed, Mar 6 2013 1:55 AM"
  },
  "array": [
    2013,
    2,
    6,
    1,
    55,
    42,
    283
  ],
  "offset": 1362534942283,
  "unix": 1362534942,
  "utc": "2013-03-06T01:55:42.283Z",
  "valid": true,
  "integer": false,
  "zone": 0
}
Cody Reichert
  • 1,620
  • 15
  • 13
Chris Ismael
  • 612
  • 5
  • 13
  • 14
6

If you're using Rails, you can just make an empty file in the public folder and use ajax to get that. Then parse the headers for the Date header. Files in the Public folder bypass the Rails stack, and so have lower latency.

Community
  • 1
  • 1
chadoh
  • 4,343
  • 6
  • 39
  • 64
  • 2
    503: Over Quota - This application is temporarily over its serving quota. Please try again later. – MrYellow Jan 23 '15 at 22:27
  • Blocked by chrome (and other modern browsers?) unless server also return additional headers allowing it. – yar1 Jun 09 '16 at 07:46
  • 1/ Required CORS headers for such a cross domain request: Access-Control-Allow-Origin: * (or your domain) Access-Control-Expose-Headers: Date 2/ Observed on Android 4.1.2: Access-Control-Expose-Headers is ignored. You will get the Content-Type header only. So for this reason avoid cross domain request. 3/ If you repeat the ajax request and the browser has cached the response, you may not receive the Date header. So if you need to perform this request several time, I suggest to add a random query string to your ajax request url to prevent caching. – figolu Jan 21 '18 at 14:27