Is there a time server that offers an API that i can grab the current eastern time from?

前端 未结 3 1761
说谎
说谎 2021-01-21 09:29

Ok we have a countdown on our site and I need to do an api call to get the current eastern time. I have this link which is the time I need. Is there any way or any webservice th

相关标签:
3条回答
  • 2021-01-21 10:02

    I use http://www.timeapi.org/ to get the current time. It can return the date/time in a variety of formats and its free and easy to use. Obviously, the server time is quite easy to get but sometimes that is not an option, which is why I have used this API on a couple occasions. You can use the pure JavaScript method that is listed on their site, but I opted for a jQuery AJAX call :

    $.ajax({
        type: "GET",
        url: 'http://www.timeapi.org/utc/now.json',
        dataType: "jsonp",
        context: this
    }).done(function(data) {
        // do stuff
    })
    .fail(function(){
        throw new Error('timeAPI ajax called failed!');
    });
    
    0 讨论(0)
  • 2021-01-21 10:05

    http://worldclockapi.com/ is helpful.

    Try this: http://worldclockapi.com/api/json/est/now

    And you may want to use the JSONP version or some sort of CORS proxy.

    0 讨论(0)
  • 2021-01-21 10:18

    The easiest answer is to just get the current time from your server, as this avoids cross-origin issues.

    • Every OS has a mechanism that syncs the local clock with a more reliable timekeeping source (such as NIST's atomic clock). If it's not already, configure your server to sync its clock with one of those sources.
    • It's relatively trivial to write a script in the language of your choice that returns the current time as a JSON object. Less trivial is ensuring that script returns the time in the proper time zone, accounting for DST (summer time) -- whether ET is UTC-5 or UTC-4. Many frameworks will take care of this for you, but some don't.

    Keep in mind that you still aren't (and can't) guaranteeing second-resolution accuracy. By requesting the time from your server, you're totally at the mercy of network conditions that you can't measure from JavaScript. One client might receive the AJAX time request in tens of milliseconds, while others with low-quality connections might even take tens of seconds.

    In fact, in many cases it might be more reliable to use the client's clock since it too will be synced to a reliable time source via NTP. I believe Windows has NTP sync enabled by default. Your best bet might be to make an AJAX request for the server's current time and date, and if the local machine's time is within 20 or so seconds of the server's time, just use local time.

    0 讨论(0)
提交回复
热议问题